Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The >> 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.
result = left_operand >> right_operand;

Integer Promotion and Return Type

Before the shift operation occurs, both operands undergo standard integer promotions:
  • Types smaller than int (such as char and short) are promoted to int (or unsigned int if the value cannot fit in an int).
  • 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 the left_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 with 0) instead.

Syntax and Behavior Visualization

#include <stdint.h>

// 1. Unsigned Right Shift (Logical Shift)
// uint32_t promotes to itself (or unsigned int), remaining unsigned.
uint32_t val_unsigned = 3221225472U; 
// Binary: 11000000 00000000 00000000 00000000

uint32_t res_unsigned = val_unsigned >> 2; 
// Binary: 00110000 00000000 00000000 00000000 (Vacated bits filled with 0)


// 2. Signed Right Shift (Implementation-Defined)
// int32_t promotes to itself (or int), remaining signed.
int32_t val_signed = -1073741824;    
// Binary: 11000000 00000000 00000000 00000000 (Two's complement)

int32_t res_signed = val_signed >> 2;  
// Binary: 11110000 00000000 00000000 00000000 (Typical: filled with sign bit 1)

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:
  1. Negative Shift Amount: The right operand is less than zero.
int x = 10 >> -1; // Undefined Behavior
  1. Oversized Shift Amount: The right operand is greater than or equal to the width (in bits) of the promoted left operand.
uint32_t y = 15;
uint32_t z = y >> 32; // Undefined Behavior (32 is >= width of uint32_t)
Master C with Deep Grasping Methodology!Learn More