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.
<< (left shift) operator is a binary bitwise operator in Java that shifts the two’s complement bit pattern of the left operand to the left by the number of positions specified by the right operand.
Mechanics
When the<< operator is applied:
- The bits of the
leftOperandare shifted to the left by the distance of therightOperand. - The vacated bit positions on the right are filled with zeros (
0). - The bits shifted off the left boundary (the most significant bits, including the sign bit) are discarded.
Type Promotion
Before the shift operation occurs, Java applies unary numeric promotion to the left operand:- If the left operand is of type
byte,short, orchar, it is implicitly promoted to a 32-bitint. The result of the shift will also be anint. - If the left operand is a
long, no promotion occurs, and the result is along.
Shift Distance Masking
To prevent undefined behavior from shifting by a distance greater than the bit-width of the operand, Java applies a bitwise AND mask to the right operand:- For
intoperands: The right operand is masked with0x1F(31). The actual shift distance isrightOperand & 31. For example, shifting anintby 32 positions is equivalent to shifting it by 0 positions. - For
longoperands: The right operand is masked with0x3F(63). The actual shift distance isrightOperand & 63.
Syntax Visualization
Example 1: Standard Left Shift Shifting the integer5 left by 2 positions.
int.
Master Java with Deep Grasping Methodology!Learn More





