std::rotr
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Bit manipulation
(C++20)
(C++23)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
rotr
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Defined in header
<bit>
template< class T >
constexpr T rotr( T x, int s ) noexcept;
(since C++20)
constexpr T rotr( T x, int s ) noexcept;
Computes the result of bitwise right-rotating the value of x by s positions. This operation is also known as a right circular shift.
Formally, let N
be std::numeric_limits <T>::digits and r be s % N.
- If r is 0, returns x;
- if r is positive, returns (x >> r) | (x << (N - r));
- if r is negative, returns std::rotl (x, -r).
This overload participates in overload resolution only if T
is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).
Contents
[edit] Parameters
x
-
value of unsigned integer type
s
-
number of positions to shift
[edit] Return value
The result of bitwise right-rotating x by s positions.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_bitops |
201907L |
(C++20) | Bit operations |
[edit] Example
Run this code
#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"; }
Output:
00011101 <- x 00011101 <- rotr(x, 0) 10001110 <- rotr(x, 1) 10001110 <- rotr(x, 9) 00111010 <- rotr(x, -1) 01000111 <- rotr(x, 2)