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 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.
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.
- If the original number is positive (sign bit is
0), the leftmost bits are padded with0s. - If the original number is negative (sign bit is
1), the leftmost bits are padded with1s.
Syntax Visualization
Because Java uses 32-bit two’s complement representation for theint data type, the bitwise operations look like this in memory:
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, orcharare implicitly promoted to a 32-bitint. - The result of the shift operation on these smaller types is always an
int.
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 asishr 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 with0x1F(31). This evaluates todistance & 31. - For
long(64-bit): The shift distance is masked with0x3F(63). This evaluates todistance & 63.
%). 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).
Master Java with Deep Grasping Methodology!Learn More





