std::rotr
来自cppreference.com
<tbody>
</tbody>
| 在标头 <bit> 定义
|
||
template< class T > constexpr T rotr( T x, int s ) noexcept; |
(C++20 起) | |
计算将 x 右旋转 s 位的结果。此运算被称为右循环移位。
正式而言,令 N 为 std::numeric_limits<T>::digits,并令 r 为 s % N。
- 若
r为0,则返回x; - 若
r为正,则返回(x >> r) | (x << (N - r)); - 若
r为负,则返回std::rotl(x, -r)。
此重载只有在 T 为无符号整数类型(即 unsigned char、unsigned short、unsigned int、unsigned long、unsigned long long 或扩展无符号整数类型)时才会参与重载决议。
参数
| x | - | 无符号整数类型的值 |
| s | - | 移位的位数 |
返回值
将 x 右旋转 s 位的结果。
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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, 9, -1, 2})
std::cout << bin(std::rotr(x, s)) << " <- rotr(x, " << s << ")\n";
}
输出:
00011101 <- x
00011101 <- rotr(x, 0)
10001110 <- rotr(x, 1)
10001110 <- rotr(x, 9)
00111010 <- rotr(x, -1)
01000111 <- rotr(x, 2)
参阅
(C++20) |
计算逐位左旋转的结果 (函数模板) |
| 进行二进制左移和右移 ( std::bitset<N> 的公开成员函数)
|