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 is a compound assignment operator that shifts the binary representation of its left operand to the left by the number of bit positions specified by its right operand, and subsequently assigns the resulting value back to the left operand.
E1 <<= E2 is equivalent to E1 = E1 << E2, with the critical distinction that the left operand E1 is evaluated exactly once. This is particularly relevant when the left operand contains side effects (e.g., array[i++] <<= 2;).
Mechanics
When the<<= operator is executed, the following bit-level operations occur:
- Bit Displacement: All bits in the left operand are moved to the left by the integer value of the right operand.
- Zero-Filling: The vacated least significant bits (LSBs) on the right side are unconditionally filled with zeros.
- Truncation: The most significant bits (MSBs) that are shifted beyond the allocated memory boundary (bit-width) of the left operand’s data type are discarded.
Type Promotion and Evaluation
Before the shift occurs, standard integer promotions are applied to both operands independently to perform the underlying bitwise shift. However, the type of the resulting assignment expression is the unpromoted type of the left operand. The right operand does not affect the type of the result. If the left operand is a type smaller thanint (such as char, short, or uint8_t), its value is promoted to int (or unsigned int) before the shift. The shift operation is performed on this promoted type, and the result is subsequently converted (truncated) back to the original unpromoted type of the left operand upon assignment.
Undefined Behavior (UB) Constraints
The C standard imposes strict limitations on the shift operation to prevent undefined behavior. The operationE1 <<= E2 invokes UB if any of the following conditions are met:
- Negative Shift Amount: The right operand
E2is less than zero. - Oversized Shift Amount: The right operand
E2is greater than or equal to the width (in bits) of the promoted left operandE1. For example, shifting a 32-bit integer by 32 or more positions is UB. - Signed Overflow (Pre-C23): If the left operand
E1has a signed type and a non-negative value, but the mathematically shifted value cannot be represented in the corresponding promoted type. - Negative Left Operand (Pre-C23): If the left operand
E1has a signed type and a negative value. (Note: C23 standardizes two’s complement representation, making left shifts on negative signed integers well-defined).
Master C with Deep Grasping Methodology!Learn More





