std::rotl
來自 cppreference.com
定義於標頭檔案 <bit> |
||
template< class T > constexpr T rotl( 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::rotr(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, 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> 的公共成員函式) |