In Java, move left (<<
) and right (>>
) is a bit operator used to shift binary numbers. The shift operation directly operates the bit mode of the number.
1. Left shift operator (<<)
Function:
- The left shift operation will move the binary bit of a number to the left by the specified number of bits, and use it on the right.0Fill.
- Each move one to the left is equivalent to multiplying the number by 2 (but not in the strict sense multiplication, which may cause overflow).
grammar:
result = value << n;
-
value
It is the number to be operated. -
n
is the number of bits that are moved.
Features:
- Moving left will expand the value of the number (if there is no overflow).
- Add 0 to the right.
- If it is a negative number, the two's calculation will be participated in the complement form.
Example:
int a = 5; // Binary: 0000000000000000000000000000000000101int result = a << 2; // 2 bits left: 0000000000000000000000000000000000000000000000000000000000000 (20)(result); // Output: 20
2. Right shift operator (>>)
Function:
- The right moves the binary bit of a number to the right by the specified number of bits.
- Sign bit expansion: If it is a positive number, the high position is filled with 0; if it is a negative number, the high position is filled with 1 (that is, the sign bit remains unchanged).
- Each move one right is equivalent to dividing the number by 2 (rounded down).
grammar:
result = value >> n;
-
value
It is the number to be operated. -
n
is the number of bits that are moved.
Features:
- For positive numbers, the high position is filled with 0 after shifting right.
- For negative numbers, the high bit is filled with 1 after shifting right (sign bit expansion).
- Suitable for handling signed numbers.
Example:
int a = 20; // Binary: 000000000000000000000000000000000000000000000000000010100int result = a >> 2; // Move right 2 bits: 0000000000000000000000000000000000000101 (5)(result); // Output: 5 int b = -20; // Binary: 111111111111111111111111111111111111111111111111111101100 (complement code representation)int result2 = b >> 2; // Right shift 2 bits: 1111111111111111111111111111111111111111111111111111111111 (-5)(result2); // Output: -5
3. Unsigned right-shift operator (>>>)
Apart from>>
, Java also providesUnsigned right-shift operator>>>
:
- Whether positive or negative, use high0Fill.
- Usually used to manipulate unsigned data.
Example:
int a = -20; // Binary: 111111111111111111111111111111111111111111111111111101100 (complement code representation)int result = a >>> 2; // Unsigned right shift by 2 bits: 001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111(result); // Output: 1073741819
Summarize
Operator | name | Function |
---|---|---|
<< |
Move left | The binary moves to the left and the right side adds 0, which is equivalent to multiplying by 2 to the n power (if it does not overflow). |
>> |
Symbols move right | The binary moves to the right, retains the sign bit, and complements the sign bit with the high (0 for positive numbers and 1 for negative numbers), which is equivalent to dividing by 2 to the n power. |
>>> |
Unsigned right | The binary moves to the right, and the high bit is always supplemented with 0, regardless of the sign bit. |
Extension: i <<= j
- In Java,
i <<= j
is a composite operator, which is equivalent toi = i << j
, indicating that it willi
The binary bit of moves to the leftj
bits, and then assign the result toi
. Coming sooni
The left shift j bit is equivalent toi
Multiply by2
ofj
To the power.
Computation process
-
i
The binary form will move to the leftj
Bit. - After shifting, use the low position0In addition, high bits may overflow (parts beyond the current data type range will be discarded).
- The final result is stored back to the variable
i
。
Example
Example 1: Move the positive number left
public class Main { public static void main(String[] args) { int i = 5; // Binary: 0000000000000000000000000000000000101 int j = 2; i <<= j; // Equivalent to i = i << j // 2 bits left: 000000000000000000 000000000000 00010100 (20) (i); // Output: 20 } }
Example 2: Move the negative number left
public class Main { public static void main(String[] args) { int i = -5; // Two's (complement code): 1111111111111111111111111111111111111111111111111111111111111111111111111 int j = 2; i <<= j; // Equivalent to i = i << j // left shift 2 bits: 1111111111111111111111111111111111111111111111111111101100 (-20) (i); // Output: -20 } }
Example 3: Possible overflow issues
public class Main { public static void main(String[] args) { int i = 1073741824; // Binary: 0100000000000000000000000000000000 int j = 1; i <<= j; // left shift 1 bit: 1000000000000000000000000000000000000000 // More than 32 bits, the result becomes negative (overflow) (i); // Output: -2147483648 } }
Note
-
The left shift is equivalent to a power of multiplied by two:
- Each shift of 1 bit to the left is equivalent to multiplying by
2
。 - For example:
5 <<= 1
Equivalent to5 * 2 = 10
,5 <<= 2
Equivalent to5 * 4 = 20
。 - Note that shifting left will not check for overflow issues, which may lead to negative or incorrect results.
- Each shift of 1 bit to the left is equivalent to multiplying by
-
Overflow problem:
- Shifting can cause high bits to be truncated, especially when the operand is close to the maximum value of the data type.
-
Applicable data types:
- The left-shift operation supports integer and long integer (
int
andlong
)。 - If the type of operation is smaller than
int
(for examplebyte
orshort
) will be promoted toint
Then perform shift operation.
- The left-shift operation supports integer and long integer (
Summarize
This is the article about Java left and right movement operations. For more information about Java left and right movement operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!