Namespaces
Variants

std::rotl

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
Defined in header <bit>
template< class T >
constexpr T rotl( T x, int s ) noexcept;
(since C++20)

Computes the result of bitwise left-rotating the value of x by s positions. This operation is also known as a left 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::rotr(x, -r).

Parameters

x - a value to shift
s - the number of positions to shift
Type requirements
T - must be an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type) in order to participate in overload resolution.

Return value

The result of bitwise left-rotating x by s positions.

Notes

Feature-test macro Value Std Feature
__cpp_lib_bitops 201907L (C++20) Bit operations

Example

#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";
}

Output:

00011101 <- x
00011101 <- rotl(x, 0)
00111010 <- rotl(x, 1)
11010001 <- rotl(x, 4)
00111010 <- rotl(x, 9)
10001110 <- rotl(x, -1)

See also

(C++20)
computes the result of bitwise right-rotation
(function template) [edit]
reverses the bits in an integer
(function template) [edit]
performs binary shift left and shift right
(public member function of std::bitset<N>) [edit]