Important: 1. Convert the binary number before making the shift,
2. Negative number to the high position to use 1, positive number 0
Displacement number << number of shifts
First convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011, then move the number high (left) two 0, the other numbers are shifted to the left 2 bits, and finally on the low (right) two empty fill 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100, then the conversion to decimal is 12.
On the premise that the number is not overflow, for positive and negative numbers, the left one is the equivalent of multiplying by 2 by 1, and the left shift n is the equivalent of multiplying by 2 of the n-th square.
>>
According to the binary form of all the numbers to the right to move the corresponding displacement number, low-shift out (discard), high position of the empty fill sign, that is, positive 0, negative 1.
Syntax format:
Number of digits >> shifts that need to be shifted
For example, one >> 2, the number 11 is shifted to the right 2 bits
Calculation process:
The binary form of 11 is: 0000 0000 0000 0000 0000 0000 0000 1011, then the last two digits of the low position are shifted out, since the number is positive, so the high is 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 0010. Conversion to decimal is 2. L >>>
Operation rules:
In binary form, all numbers are shifted to the right by the corresponding number of digits, the low position is shifted out (discarded), and the upper slot is 0. For positive numbers, it is the same as a signed right shift, which is different for negative numbers.
Java Shift Operators