std::rotr(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::rotr(3) β€” C++ Standard Library

okay bestie, welcome to the std::rotr page β€” yes, that's the C++ Standard Library, and yes, this little icon is about to make bit rotation make sense. buckle up πŸŽ€


✨ NAME ✨

std::rotr β€” literally std::rotr. that's the name, that's the vibe, no notes πŸ’–


🌸 SYNOPSIS 🌸

Defined in header <bit> β€” that's where you go to find it, mwah.

template< class T >                                     (since C++20)
[[nodiscard]] constexpr T rotr( T x, int s ) noexcept;

So what does it actually do? It computes the result of bitwise right-rotating the value of x by s positions. This operation is also known as a right circular shift πŸ”„βœ¨ (circular! like a little hamster wheel of bits, iykyk).

Formally β€” and bestie, we love a formal definition β€” let N be std::numeric_limits<T>::digits, and let r be s % N. Then:

  • If r is 0, it returns x;
  • if r is positive, it returns (x >> r) | (x << (N - r));
  • if r is negative, it returns std::rotl(x, -r).
🫢 lowkey important: 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). no signed shenanigans, we don't do that here πŸ’…

πŸ¦‹ PARAMETERS πŸ¦‹

  • x β€” value of unsigned integer type (the bit baddie in question πŸ‘‘)
  • s β€” number of positions to shift (how many spins we're doing)

⭐ RETURN VALUE ⭐

The result of bitwise right-rotating x by s positions. that's it, that's the whole gift 🎁


πŸ’œ NOTES πŸ’œ

Feature-test macro check, periodt β€” here's the table, done as a list because we're cute like that:

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

🌈 EXAMPLE 🌈

// Run this code

#include <bit>
#include <bitset>
#include <cstdint>
#include <iostream>

int main()
{
    const std::uint8_t i = 0b00011101;
    std::cout << "i          = " << std::bitset<8>(i) << '\n';
    std::cout << "rotr(i,0)  = " << std::bitset<8>(std::rotr(i, 0)) << '\n';
    std::cout << "rotr(i,1)  = " << std::bitset<8>(std::rotr(i, 1)) << '\n';
    std::cout << "rotr(i,9)  = " << std::bitset<8>(std::rotr(i, 9)) << '\n';
    std::cout << "rotr(i,-1) = " << std::bitset<8>(std::rotr(i, -1)) << '\n';
}

πŸ“ Output

i          = 00011101
rotr(i,0)  = 00011101
rotr(i,1)  = 10001110
rotr(i,9)  = 10001110
rotr(i,-1) = 00111010

look at that. 9 and 1 give the same answer because 9 % 8 == 1 β€” she's consistent, she's iconic, she ate πŸ”₯


🧚 SEE ALSO 🧚

  • rotl β€” computes the result of bitwise left-rotation (C++20) (function template) β€” the mirror twin, obsessed 😭
  • operator<<=, operator>>=, operator<<, operator>> β€” performs binary shift left and shift right (public member function of std::bitset<N>)

and that's the page, bestie! 🌟 brought to you by http://cppreference.com, dated 2024.06.10, filed under std::rotr(3) β€” go rotate some bits responsibly, ily, bye πŸ’…πŸŽ€πŸ’–

πŸ“¦ where this page comes from