Java Shift Operators
Java shift operators are essentially three types: <(left shift),> (shifts right with symbols), and> (shifts right without symbols ).
1. Left Shift OperatorLeft shift operator < <使指定值的所有位都左移规定的次数。
1) its general format is as follows:
Value <num
Num specifies the number of digits in which the value to be shifted is moved.
The rule of Left shift only remembers one point: discard the highest bit (the symbol bit is also discarded), 0 fill the second bit
If the number of digits to be moved exceeds the maximum number of digits of this type, the compiler will modulo the number of digits to be moved. For example, if the int type is moved to 33 bits, only 332 = 1 bits are actually moved.
2) operation rules
In binary format, all numbers are moved to the right to the corresponding number of digits. The high position is removed (discarded), and the low position is filled with zero.
When the number of operations to the Left shift is int type, every 31st bits to be moved will be removed and discarded;
When the number of left-shifted operations is long, the first 63rd bits are removed and discarded.
When the number of left-shift operations is of the byte and short types, these types are automatically extended to the int type.
3) mathematical significance
If the number does not overflow, for positive and negative numbers, moving one to the left is equivalent to multiplying the 1 power of 2, and moving n to the left is equivalent to multiplying the n power of 2.
4) computing process:
Example: 3 <2 (3 is int type)
1) Convert 3 to a binary number 0000 0000 0000 0000 0000 0000 0000 0011,
2) Remove the two zeros at the upper (left) of the number. All the other digits are shifted to the left.
3) Fill in the two vacant positions at the low position (right side. The final result is 0000 0000 0000 0000 0000 0000 0000 1100,
Convert to 12 in decimal format.
The number of digits to be moved exceeds the maximum number of digits of this type,
If you move to a higher-order bit (31 or 63 bits), the value changes to a negative value. The following program illustrates this:
// Left shifting as a quick way to multiply by 2.
Public class MultByTwo {
Public static void main (String args []) {
Int I;
Int num = 0 xFFFFFFE;
For (I = 0; I <4; I ){
Num = num <1;
System. out. println (num );
}
}
}
The program output is as follows:
536870908
1073741816
2147483632
-32
Note: n-bit binary, with the highest bit as the symbol bit. Therefore, the value range is-2 ^ (n-1 )~ 2 ^ (n-1)-1, so the modulo is 2 ^ (n-1 ).
2. Right Shift OperatorShift right operator < <使指定值的所有位都右移规定的次数。
1) its general format is as follows:
Value> num
Num specifies the number of digits in which the value to be shifted is moved.
Remember only one point for the right shift rule: The symbol bit remains unchanged, and the symbol bit is added on the left.
2) operation rules:
In binary format, all numbers are moved to the right to the corresponding number of digits. The low position is removed (discarded). The high position is filled with the sign bit, that is, the positive number is supplemented with zero, and the negative number is supplemented with 1.
When the right shift operation is of the byte and short types, these types are automatically extended to the int type.
For example,If the value to be removed is a negative number, 1 is added on the left for each right shift. If the value to be removed is a positive number, 0 is added on the left for each right shift, this is called the symbol bit extension (reserve the symbol bit)(Sign extension), right shift in progress
This operation is used to maintain the negative number.
3) mathematical significance
Shifts one to the right is equivalent to dividing 2, and shifts n to the right is equal to dividing by 2 to the Npower.
4) computing process
11> 2 (11 is int type)
1) 11 binary format: 0000 0000 0000 0000 0000 0000 0000 1011
2) remove the last two digits of the low position. Because the number is positive, it is set to zero at the high position.
3) the final result is 0000 0000 0000 0000 0000 0000 0000 0010.
Convert to 3 in decimal format.
35> 2 (35 is int type)
35 to binary: 0000 0000 0000 0000 0000 0000 0010 0011
Remove the last two digits of the low position: 0000 0000 0000 0000 0000 0000 0000 1000
Convert to decimal: 8
3. unsigned right shift operator >>>
Its common format is as follows:
Value >>> num
Num specifies the number of digits in which the value to be shifted is moved.
The rules for unsigned right shift only remember one point: Ignore the symbol bit extension, 0 fill the highest bit
Unsigned right shift operator >>> only valid for 32-bit and 64-bit values