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 Java is the signed right shift bitwise operator. It shifts the bit pattern of the left operand to the right by the number of positions specified by the right operand, while performing sign extension to preserve the sign of the original number.
result = value >> distance;

Mechanics of the Shift

When the >> operator is applied, the bits of the value are moved to the right.
  • The bits shifted off the right side (the least significant bits, or LSB) are permanently discarded.
  • The empty spaces created on the left side (the most significant bits, or MSB) are filled with the value of the original sign bit.
This behavior is known as sign extension:
  • If the original number is positive (sign bit is 0), the leftmost bits are padded with 0s.
  • If the original number is negative (sign bit is 1), the leftmost bits are padded with 1s.

Syntax Visualization

Because Java uses 32-bit two’s complement representation for the int data type, the bitwise operations look like this in memory:
// Positive Integer Shift
int a = 8;      // 00000000 00000000 00000000 00001000
int b = a >> 2; // 00000000 00000000 00000000 00000010 (Result: 2)

// Negative Integer Shift
int c = -8;     // 11111111 11111111 11111111 11111000
int d = c >> 2; // 11111111 11111111 11111111 11111110 (Result: -2)

Type Promotion and Evaluation

The >> operator only operates on integral types (byte, short, char, int, long). Before the shift occurs, Java applies unary numeric promotion:
  • Operands of type byte, short, or char are implicitly promoted to a 32-bit int.
  • The result of the shift operation on these smaller types is always an int.
byte b = -16;
// 'b' is promoted to int: 11111111 11111111 11111111 11110000
// The result is an int, not a byte.
int result = b >> 2; 

Shift Distance Masking

Java restricts the shift distance to prevent shifting by more bits than the data type holds. At runtime, the JVM (via bytecodes such as ishr or lshr) or the underlying CPU hardware applies a bitwise AND mask to the right operand (distance) based on the type of the promoted left operand:
  • For int (32-bit): The shift distance is masked with 0x1F (31). This evaluates to distance & 31.
  • For long (64-bit): The shift distance is masked with 0x3F (63). This evaluates to distance & 63.
This bitwise masking is strictly distinct from Java’s remainder operator (%). If the remainder operator were used, a negative shift distance would yield a negative result (e.g., -1 % 32 == -1). By using a bitwise AND, Java ensures the shift distance correctly wraps to a positive integer (e.g., -1 & 31 == 31).
int value = 1024;

// 34 is masked to (34 & 31), which equals 2.
// The operation executed at runtime is value >> 2.
int result = value >> 34; 

// -1 is masked to (-1 & 31), which equals 31.
// The operation executed at runtime is value >> 31.
int negativeShift = value >> -1; 
Master Java with Deep Grasping Methodology!Learn More