std::rotr(3)
π 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
ris0, it returnsx; - if
ris positive, it returns(x >> r) | (x << (N - r)); - if
ris negative, it returnsstd::rotl(x, -r).
π«Ά lowkey important: this overload participates in overload resolution only ifTis 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 ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms