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.
<<= (bitwise left shift assignment) operator shifts the binary representation of its left operand to the left by the number of bit positions specified by its right operand, and assigns the resulting value back to the left operand.
lhs <<= rhs is equivalent to lhs = static_cast<T>(lhs << rhs) (where T is the original type of lhs), with the strict guarantee that the expression lhs is evaluated exactly once. The expression evaluates to an lvalue reference to the left operand (lhs), retaining its original, unpromoted type.
Operand Requirements
The left operand (lhs) must be a modifiable lvalue of an integral type. The right operand (rhs) must be of an integral or unscoped enumeration type.
Prior to the shift, the compiler performs standard integral promotions on both operands. The intermediate shift operation is performed using the promoted type of the left operand, but the final result is converted back to the original, unpromoted type of lhs upon assignment. An unscoped enumeration cannot be used as the left operand because the promoted integer result of the shift cannot be implicitly assigned back to an enumeration type.
Bit-Level Mechanics
- Shift Direction: The bits of
lhsare moved to the left byrhspositions. - Zero Extension: The vacated bit positions on the right (least significant bits) are filled with zeros.
- Truncation: Because the result is assigned back to
lhs, bits that are shifted beyond the most significant bit (MSB) of the original, unpromoted type oflhsare discarded during the assignment conversion.
Undefined Behavior (UB)
The<<= operator will invoke undefined behavior under the following conditions:
- The right operand (
rhs) is strictly negative. - The right operand (
rhs) is greater than or equal to the total number of bits in the promoted type of the left operand (lhs).
C++20 Standard Changes
The behavior of<<= on signed integers changed significantly in C++20 due to the mandated adoption of two’s complement representation:
- Pre-C++20: Left-shifting a negative signed integer, or left-shifting a positive signed integer such that the shifted value cannot be represented in the result type (overflow), resulted in undefined behavior.
- C++20 and later: Left-shifting signed integers is fully well-defined. The operation simply discards the bits shifted past the MSB and zero-fills from the right, behaving identically to the corresponding unsigned integer type. Mathematical overflow no longer triggers undefined behavior for this operator.
Master C++ with Deep Grasping Methodology!Learn More





