std::rotl

来自cppreference.com
< cpp‎ | numeric
 
 
 
 
在标头 <bit> 定义
template<class T >
constexpr T rotl( T x, int s )noexcept;
(C++20 起)

计算将 x 左旋转 s 位的结果。此运算也称为左循环移位

正式而言,令 Nstd::numeric_limits<T>::digits,并令 rs % N

  • r0,则返回 x
  • r 为正,则返回 (x << r)|(x >>(N - r))
  • r 为负,则返回 std::rotr(x, -r)

此重载只有在 T 为无符号整数类型(即 unsignedcharunsignedshortunsignedintunsignedlongunsignedlonglong 或扩展无符号整数类型)时才会参与重载决议。

目录

[编辑]参数

x - 无符号整数类型的值
s - 移位的位数

[编辑]返回值

x 左旋转 s 位的结果。

[编辑]注解

功能特性测试标准功能特性
__cpp_lib_bitops201907L(C++20)位运算

[编辑]示例

#include <bit>#include <bitset>#include <cstdint>#include <iostream>   int main(){using bin =std::bitset<8>;conststd::uint8_t x{0b00011101};std::cout<< bin(x)<<" <- x\n";for(constint s :{0, 1, 4, 9, -1})std::cout<< bin(std::rotl(x, s))<<" <- rotl(x, "<< s <<")\n";}

输出:

00011101 <- x 00011101 <- rotl(x, 0) 00111010 <- rotl(x, 1) 11010001 <- rotl(x, 4) 00111010 <- rotl(x, 9) 10001110 <- rotl(x, -1)

[编辑]参阅

(C++20)
计算逐位右旋转的结果
(函数模板)[编辑]
进行二进制左移和右移
(std::bitset<N> 的公开成员函数)[编辑]
close