std::rotr

從 cppreference.com
< cpp‎ | numeric
 
 
 
位操縱
(C++20)
(C++23)
二的整數次冪
(C++20)
(C++20)
(C++20)
旋轉
(C++20)
rotr
(C++20)
計數
(C++20)
(C++20)
(C++20)
端序
(C++20)
 
在標頭 <bit> 定義
template< class T >
constexpr T rotr( 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::rotl(x, -r)

此重載只有在 T 為無符號整數類型(即 unsigned charunsigned shortunsigned intunsigned longunsigned 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> 的公開成員函數) [編輯]