<<
-
<<: Left shift operation, a few left to fill a few 0
>>
-
>>: Right shift operation, is arithmetic right shift
- If the number is positive, the shift is followed by adding 0 in front
-
If the number is negative, the shift is followed by a 1 in front
>>>
- > > > : Unsigned right shift is logical right shift. Ignore the sign. The empty space fills the 0
- Unsigned right shift rule: Ignoring sign bit extension,0 fills in the highest bit, and the unsigned right shift operator >>> is only meaningful for 32-bit and 64-bit values
-
If the number to be shifted is positive:
- The value of a right shift is the same as an unsigned right shift
-
If the number to be shifted is negative:
- It’s still going to be negative
- The unsigned value moved to the right is positive
-
The difference between:
- For positive numbers, there’s no difference between >,> and >,>,>
-
For negative numbers, the sign bit of the highest digit is ignored because of the unsigned right shift. So:
- -2 >>> 1 = 2147483647(Integer.MAX_VALUE)
- -1 >>> 1 = 2147483647(Integer.MAX_VALUE)
-
So to determine if the symbol of two INTEGER types is the same, you can say:
return ((a >> 31) ^ (b >> 31)) == 0;
Result of shift operation
- When a shift is not greater than the largest number of digits of its own numeric type, when a number is moved to the left by n digits, it is multiplied by 2 to the NTH power
-
When you move a number n places to the right, you divide that number by 2 to the NTH power, and then you round it
-
If the number of moves exceeds the maximum number of its own value type, you simply apply the number obtained by taking the remainder of the shift number and the maximum number of its own value type
Pay attention to
-
-
The three shift operations operate on five left-hand operands:
- long
- int
- short
- byte
- char
-
The specific operation process is different for different operand types. Follow the following principles:
-
When an int is shifted, the left operand is 32 bits, and the shift symbol is applied to the 32-bit bit
- For example, 1 >> 3 moves the 32 bits of 00000000 00000000 0001 to the right by 3 bits
- When a long is shifted, the left operand is 64 bits, and the shift symbol is applied to the 64-bit bit
-
Short,byte, and char first convert the data to an int before the shift, and then shift again, where the shift symbol is applied to the 32-bit bit
- (byte)0xff >>> 7 (byte)0xff >>> 7 (byte)0xff >>> 7 (byte)0xff >>> 7
-
-
From the above we can know:
- When the left-hand operand is long, the shift results in the type long
-
When the left operand is of the other four types, the shift results in an int
- So when the left operand is short,byte, and char, use >>=, >>>=, <<=. It’s the value of the int that you’re truncating, and you’re always going to get the wrong value
-
The three shift symbols have rules for the right-hand operand as well as the left-hand operand:
-
If the left-hand operand is an int or if the conversion is followed by an int, then the right-hand operand is only valid in the lower five bits, because ints are 32 bits in total
- 22 >> 33 and 22 >, > 1 are the same thing, they’re both 11
- If the left-hand operand is long, then only the lower six bits of the right-hand operand are valid
-