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.
>> operator in C is the bitwise right shift operator. It shifts the binary representation of its promoted left operand to the right by the number of bit positions specified by its right operand. Bits shifted off the least significant end are discarded.
Integer Promotion and Return Type
Before the shift operation occurs, both operands undergo standard integer promotions:- Types smaller than
int(such ascharandshort) are promoted toint(orunsigned intif the value cannot fit in anint). - The return type of the
>>operation is exactly the type of the promoted left operand. The type or value of the right operand does not affect the return type.
Mechanics and Bit Filling
When bits are shifted to the right, vacated bit positions are created at the most significant bit (MSB) end. How C fills these vacated bits depends strictly on the promoted type of theleft_operand:
- Unsigned Promoted Types (Logical Shift): If the promoted left operand has an unsigned type, the operator performs a logical right shift. The vacated MSBs are unconditionally filled with zeros (
0). - Signed Promoted Types (Arithmetic vs. Logical Shift): If the promoted left operand has a signed type, the behavior depends on its value:
- Non-negative values: The vacated MSBs are filled with zeros (
0). - Negative values: The behavior is implementation-defined. Most modern C compilers (such as GCC and Clang on x86/ARM architectures) perform an arithmetic right shift, filling the vacated bits with the sign bit (
1) to preserve the two’s complement sign. However, the C standard permits compilers to perform a logical shift (filling with0) instead.
- Non-negative values: The vacated MSBs are filled with zeros (
Syntax and Behavior Visualization
Undefined Behavior (UB) Constraints
The C standard dictates that the>> operator invokes Undefined Behavior if any of the following conditions are met regarding the right_operand:
- Negative Shift Amount: The right operand is less than zero.
- Oversized Shift Amount: The right operand is greater than or equal to the width (in bits) of the promoted left operand.
Master C with Deep Grasping Methodology!Learn More





