The object of the shift operator operation is the binary bit, which can be used to handle int integers with the shift operator alone.
| Operator |
Meaning |
Example |
| << |
Left-shift operator, moves the left-hand object to the left by the number of digits specified to the right of the operator (in the low-fill 0) |
X<<3 |
| >> |
The signed right-shift operator moves the object to the left of the operator to the right by the number of digits specified to the right of the operator. Use the symbol extension mechanism, that is, if the value is positive, then the high is 0, and if the value is negative, the high is 1. |
X>>3 |
| >>> |
The "unsigned" right-shift operator moves the object to the left of the operator to the right by the number of digits specified to the right of the operator. Use 0 expansion mechanism, that is, regardless of the value of the positive or negative, are in the high 0. |
X>>>3 |
In Java, Byte,short,long converts the respective type to int when the shift operation is converted to a binary calculation and converts it to the corresponding type.
such as: 3<<3 = 0000 0011 * 2^3 = 0001 1000 = 24, i.e. move left three bit, low 0
>> 3 = 0001 1000/2^3 = 0000 0011 = 3, that is, move to the right three bits, high fill 0
X<<y equivalent to x*2y, x>>y equivalent to x/2y
From the computational speed, the shift operation is faster than the arithmetic operation.
If x is negative, then x>>>3 has no arithmetic meaning, only logical meaning.
In Java, the data length of type int is 32 bits, and if you move the int type to the left or right by a value greater than or equal to 32 bits, the data is not populated as expected by 1 or 0. Java is handled in the following way: When the length of the data is exactly the number of times, that is, 32, 64, the data remains unchanged, in other cases, the length of the move divided by 32. In the same vein, the long type data has a 64 change benchmark.
In the practical application, it is necessary to pay special attention to this point, of course, you can use this feature to implement some special algorithm design.
Java Shift Operations