TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
>>= (right shift assignment) operator is a compound assignment operator that performs a bitwise right shift on its left operand by the number of bit positions specified by its right operand, subsequently assigning the computed result back to the left operand.
lhs >>= rhs is equivalent to lhs = lhs >> rhs, with the strict guarantee that the expression lhs is evaluated exactly once.
Operand Constraints
The operands have distinct type requirements:- Left Operand (
lhs): Must be a modifiable lvalue of an integral type (e.g.,char,short,int,long,unsigned). It cannot be an enumeration type, because C++ does not allow implicit conversions from the integer result of the shift operation back to an enumeration. - Right Operand (
rhs): Must be of an integral type or an unscoped enumeration type (which implicitly promotes to an integer).
>>) applies standard integer promotions to the operands before the shift operation occurs, the compound assignment operator converts the computed result back to the original type of lhs. The final result of the expression is an lvalue reference to the unpromoted left operand.
Bit-Level Behavior
The treatment of the vacated Most Significant Bits (MSBs) depends strictly on the signedness of the left operand and the C++ standard version:- Unsigned Integers: The operator performs a logical right shift. The vacated MSBs are unconditionally filled with zeros.
- Signed Integers (Non-negative values): The operator performs a logical right shift, filling vacated MSBs with zeros.
- Signed Integers (Negative values):
- C++20 and later: The operator is guaranteed to perform an arithmetic right shift. It utilizes sign-extension, filling the vacated MSBs with
1s (the original sign bit). This is mathematically defined as division by rounding towards negative infinity. - Pre-C++20: The behavior of right-shifting a negative signed integer is implementation-defined (though the vast majority of compilers historically implemented sign-extension).
- C++20 and later: The operator is guaranteed to perform an arithmetic right shift. It utilizes sign-extension, filling the vacated MSBs with
Undefined Behavior (UB)
The>>= operator invokes undefined behavior under the following conditions:
- Negative Shift Count: The right operand (
rhs) is less than0. - Oversized Shift Count: The right operand (
rhs) is greater than or equal to the total number of bits in the promoted left operand. For example, iflhspromotes to a 32-bit integer,lhs >>= 32andlhs >>= 33result in undefined behavior.
Return Value
The operator returns an lvalue reference to the modified left operand (lhs), allowing for operator chaining (e.g., a >>= b >>= c). Such chaining evaluates right-to-left due to the operator’s right-associativity.
Overloading
For user-defined types (classes or structs), the>>= operator can be overloaded by defining a function named operator>>=. When overloaded, the operator evaluates its operands exactly once as function arguments, preserving the standard single-evaluation guarantee.
Master C++ with Deep Grasping Methodology!Learn More





