CKTIS / C++
Right-shift operator >>
The built-in >> operator shifts an integer's bits to the right. In C++23, its result corresponds to division by a power of two, rounded toward negative infinity. The number of positions shifted must be within the permitted range.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
value >> countBuilt-in value >> count shifts bits to the right. Operands must have integral or unscoped enumeration types; integral promotions apply. The result has the promoted left operand's type. For example, unsigned char promotes to int if int holds all its values, or otherwise to unsigned int.
In C++23, the result is value divided by 2 raised to count, rounded toward negative infinity. Unsigned types receive zeros on the left; signed types preserve the sign. Thus -9 >> 1 produces -5, whereas -9 / 2 produces -4: integer division discards the fractional part toward zero.
A negative count, or a count at least as large as the promoted left operand's width, causes undefined behavior. Validate the range before shifting; do not assume the hardware will safely constrain the count.
Do not confuse shifting with overloaded >> in std::cin >> value: there the operator reads data from a stream. The example demonstrates an unsigned shift and the difference between shifting and dividing a negative number.
The supplied standard URL points to an evolving draft, not a frozen C++23 edition. The example targets compilation with -std=c++23. The displayed source was run unchanged in an isolated environment, and its standard output was verified to match the stated output exactly. The production runtime has not been attested.
Examples
#include <iostream>
int main() {
constexpr unsigned value = 40u;
constexpr unsigned count = 3u;
static_assert((value >> count) == 5u);
static_assert((-9 >> 1) == -5);
std::cout << (value >> count) << '\n';
std::cout << (-9 >> 1) << ' ' << (-9 / 2) << '\n';
}
Result
5 -5 -4
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.