std::rotl
来自 cppreference.cn
定义于头文件 <bit> |
||
template< class T > constexpr T rotl( T x, int s ) noexcept; |
(since C++20) | |
计算将 x 的值按位左旋转 s 位的结果。此操作也称为左循环移位。
形式上,设 N 为 std::numeric_limits<T>::digits,r 为 s % N。
- 如果 r 为 0,则返回 x;
- 如果 r 为正数,则返回 (x << r) | (x >> (N - r));
- 如果 r 为负数,则返回 std::rotr(x, -r)。
此重载仅在 T 是无符号整数类型(即 unsigned char、unsigned short、unsigned int、unsigned long、unsigned long long 或扩展无符号整数类型)时参与重载解析。
内容 |
[编辑] 参数
x | - | 无符号整数类型的值 |
s | - | 要移位的位数 |
[编辑] 返回值
将 x 按位左旋转 s 位的结果。
[编辑] 注释
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_bitops |
201907L |
(C++20) | 位操作 |
[编辑] 示例
运行此代码
#include <bit> #include <bitset> #include <cstdint> #include <iostream> int main() { using bin = std::bitset<8>; const std::uint8_t x{0b00011101}; std::cout << bin(x) << " <- x\n"; for (const int 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> 的公共成员函数) |