1. Unsigned right shift >>> or >>> =
Unsigned right Shift (>>>) is not the same as the right shift (>>) operator.
The right shift does not change the positive or negative of the number.
For a positive number, the unsigned right shift does not become a negative number (which is equivalent to dividing by 1), but for a negative number, the unsigned right shifts the negative number to a positive number;
int i =-4;
System.out.printf ("%-10d%32s\n", I, integer.tobinarystring (i));
I >>>= 1; Unsigned right shift 1 bit
System.out.printf ("%-10d%32s\n", I, integer.tobinarystring (i));
I >>>= 1;
System.out.printf ("%-10d%32s\n", I, integer.tobinarystring (i));
Output:
-4 11111111111111111111111111111100//negative numbers are stored in the form of complement in Java, and 4 of the complement means 11111111111111111111111111111100
2147483646 1111111111111111111111111111110//Unsigned Right shift 1 bit,-4 of the symbol bit also shifted right by 1 bits, resulting in the sign bit becoming 0, becoming a positive number
1073741822 111111111111111111111111111110//Unsigned right shift 1 bit
int i =; System.out.printf ("%-10d%32s\n">>>= 1; System.out.printf ("%-10d%32s\n", I, integer.tobinarystring (i));
Output:
15 1111
7 111//Unsigned Right shift for positive number, high 0
int i = 0x80000000; System.out.printf ("%-12d%32s\n">>>= 1; System.out.printf ("%-12d%32s\n">>>= 1; System.out.printf ("%-12d%32s\n", I, integer.tobinarystring (i));
Output:
-2147483648 10000000000000000000000000000000//minimum negative for the complement representation
1073741824 1000000000000000000000000000000//sign bit right shift one bit, turn positive
536870912 100000000000000000000000000000
Summarize:
The term "Unsigned right shift" is easy to misunderstand. Although called the unsigned right-shift operation, the first impression is that the symbol bit is not shifted, but in fact, with the symbol bit to the right shift;
Unsigned right shifts are made to the complex numbers, and the sign bits move together to the right, and will become positive.
Several unsigned right shifts to positive numbers are always positive or 0.
2. Left shift operation << or <<=
Unlike the right-shift operation, unsigned left and left shifts are the same. Therefore, Java does not have unsigned left-shift operations. (<<< and <<<= will error)
Because there is no right-shift operation to consider the right shift of the sign bit, the sign bit only exists on the leftmost side of the binary representation, not at the far right.
So you don't have to differentiate between unsigned left and left shift operations.
int i = 0x80000000; System.out.printf ("%-12d%32s\n"<<= 1; System.out.printf ("%-12d%32s\n">>= 1; System.out.printf ("%-12d%32s\n", I, integer.tobinarystring (i));
Output:
-2147483648 10000000000000000000000000000000
0 0//Minimum negative number shift left 1 bit, will become 0
0 0
Unsigned shift operations in Java