Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/rotr"

From cppreference.com
< cpp‎ | numeric
(- nodiscard)
m (fmt.)
 
Line 6: Line 6:
 
}}
 
}}
  
Computes the result of bitwise right-rotating the value of {{c|x}} by {{c|s}} positions. This operation is also known as a right [[enwiki:Circular_shift|circular shift]].
+
Computes the result of bitwise right-rotating the value of {{c|x}} by {{c|s}} positions. This operation is also known as a right {{enwiki|Circular shift|circular shift}}.
  
Formally, let {{tt|N}} be {{c|std::numeric_limits<T>::digits}}, {{tt|r}} be {{c|s % N}}.
+
Formally, let {{tt|N}} be {{c/core|std::numeric_limits<T>::digits}} and {{c|r}} be {{c|s % N}}.
* If {{tt|r}} is 0, returns {{c|x}};
+
* If {{c|r}} is {{c|0}}, returns {{c|x}};
* if {{tt|r}} is positive, returns {{c|(x >> r) {{!}} (x << (N - r))}};
+
* if {{c|r}} is positive, returns {{c|(x >> r) {{!}} (x << (N - r))}};
* if {{tt|r}} is negative, returns {{c|std::rotl(x, -r)}}.
+
* if {{c|r}} is negative, returns {{c|std::rotl(x, -r)}}.
  
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c|unsigned char}}, {{c|unsigned short}}, {{c|unsigned int}}, {{c|unsigned long}}, {{c|unsigned long long}}, or an extended unsigned integer type)}}.
+
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c/core|unsigned char}}, {{c/core|unsigned short}}, {{c/core|unsigned int}}, {{c/core|unsigned long}}, {{c/core|unsigned long long}}, or an extended unsigned integer type)}}.
  
 
===Parameters===
 
===Parameters===
Line 25: Line 25:
  
 
===Notes===
 
===Notes===
{{feature test macro|__cpp_lib_bitops|std=C++20|value=201907L|[[cpp/numeric#Bit_manipulation (since C++20)|Bit operations]]}}
+
{{feature test macro|__cpp_lib_bitops|std=C++20|value=201907L|[[cpp/numeric#Bit manipulation (since C++20)|Bit operations]]}}
  
=== Example ===
+
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
Line 37: Line 37:
 
int main()
 
int main()
 
{
 
{
     const std::uint8_t i = 0b00011101;
+
     using bin = std::bitset<8>;
     std::cout << "i          = " << std::bitset<8>(i) << '\n';
+
     const std::uint8_t x{0b00011101};
     std::cout << "rotr(i,0) = " << std::bitset<8>(std::rotr(i, 0)) << '\n';
+
     std::cout << bin(x) << " <- x\n";
     std::cout << "rotr(i,1)  = " << std::bitset<8>(std::rotr(i, 1)) << '\n';
+
     for (const int s : {0, 1, 9, -1, 2})
    std::cout << "rotr(i,9)  = " << std::bitset<8>(std::rotr(i, 9)) << '\n';
+
        std::cout << bin(std::rotr(x, s)) << " <- rotr(x, " << s << ")\n";
    std::cout << "rotr(i,-1) = " << std::bitset<8>(std::rotr(i, -1)) << '\n';
+
 
}
 
}
 
|output=
 
|output=
i          = 00011101
+
00011101 <- x
rotr(i,0) = 00011101
+
00011101 <- rotr(x, 0)
rotr(i,1) = 10001110
+
10001110 <- rotr(x, 1)
rotr(i,9) = 10001110
+
10001110 <- rotr(x, 9)
rotr(i,-1) = 00111010
+
00111010 <- rotr(x, -1)
 +
01000111 <- rotr(x, 2)
 
}}
 
}}
  
=== See also ===
+
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc inc|cpp/numeric/dsc rotl}}
 
{{dsc inc|cpp/numeric/dsc rotl}}

Latest revision as of 13:22, 2 November 2024

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

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

#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)

[edit] See also

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