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 right shift assignment) operator is a compound assignment operator that shifts the binary representation of its evaluated left operand to the right by the number of bit positions specified by its right operand, and subsequently assigns the resulting value back to the left operand.
Syntax
>>=), the left_operand is evaluated exactly once. This is strictly enforced to prevent multiple evaluations of side effects (e.g., arr[i++] >>= 2;).
Technical Mechanics
Operand Constraints and Result Type: Both operands must be of integer types. Before the shift operation occurs, both operands undergo standard integer promotions. However, unlike the standalone>> operator (where the result type is the promoted type of the left operand), the type of the resulting >>= assignment expression is the unqualified type of the left operand (its original type prior to integer promotion). For example, if c is a uint8_t, the expression c >>= 2 yields a uint8_t, whereas c >> 2 yields an int.
Integer Promotion and Bit Filling Behavior:
The bits vacated at the most significant bit (MSB) positions are filled based on the type and value of the left operand after integer promotion:
-
Promoted Unsigned Types: If the promoted left operand has an unsigned type, a logical right shift is performed. The vacated MSBs are strictly filled with
0s. -
Promoted Signed Types (Non-negative): If the promoted left operand has a signed type and a positive value (or zero), the vacated MSBs are filled with
0s. Crucially, narrow unsigned types (such asuint8_torunsigned short) are typically promoted to signedint. Because their promoted values are guaranteed to be positive, the arithmetic shift fills the vacated bits with0s, effectively mirroring the behavior of a logical shift. -
Promoted Signed Types (Negative): If the promoted left operand has a signed type and a negative value, the behavior is implementation-defined. Virtually all modern C compilers implement this as an arithmetic right shift (sign extension), where the vacated MSBs are filled with
1s to preserve the negative sign.
Undefined Behavior (UB)
The>>= operator will invoke undefined behavior under the following conditions:
- The
right_operandis strictly negative. - The
right_operandis greater than or equal to the width (in bits) of the promotedleft_operand. For example, shifting a 16-bitshortright by 16 positions is valid if it promotes to a 32-bitint, but shifting a 32-bitintright by 32 or more positions is UB.
Execution Example
Master C with Deep Grasping Methodology!Learn More





