Home > Java > javaTutorial > Detailed explanation of java shift operator

Detailed explanation of java shift operator

高洛峰
Release: 2016-12-16 17:00:18
Original
1676 people have browsed it

Java shift operators are nothing more than these three: << (left shift), >> (signed right shift) and >>> (unsigned right shift).
1. Left shift operator
Left shift operator << Shifts all bits of the specified value to the left a specified number of times.
1) Its general format is as follows:
value << num
num specifies the number of bits to shift the value value.
Just remember one thing about the rules of left shift: discard the highest bit and fill the lowest bit with 0
If the number of bits moved exceeds the maximum number of bits of the type, the compiler will take modulo of the number of bits moved. For example, if the int type is moved by 33 bits, only 332=1 bit is actually moved.

2) Operation rules
Move all numbers to the left by the corresponding number of digits in binary form, shift out the high bits (discard), and fill the empty bits in the low bits with zeros.
When the operand of the left shift is of type int, every time it moves 1 bit, its 31st bit will be shifted out and discarded;
When the operand of the left shift is of type long, every time it moves 1 bit, its 63rd bit will be to be removed and discarded.
When the left-shift operand is of byte and short type, these types will be automatically expanded to int type.

3) Mathematical meaning
On the premise that the number does not overflow, for positive and negative numbers, shifting one position to the left is equivalent to multiplying by 2 raised to the power of 1, and shifting n bits to the left is equivalent to multiplying by 2 raised to the nth power.

4) Calculation process:
For example: 3 <<2 (3 is int type)
1) Convert 3 into a binary number 0000 0000 0000 0000 0000 0000 0000 0011,
2) Convert the high bit of the number (left side) ) are moved out, and all other numbers are shifted 2 bits to the left.
3) Fill in the two empty positions with zeros in the low bits (right). The final result obtained is 0000 0000 0000 0000 0000 0000 0000 1100, which converted to decimal is 12.

The number of bits moved exceeds the maximum number of bits for the type.

If the number is shifted into higher-order bits (31 or 63 bits), the value will become negative. 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 = 0xFFFFFFE;
for(i=0; i<4; i ) {
num = num << 1;
System.out.println(num);
}
}
}

The output of this program is as follows:

536870908

1073741816
2147483632
-32
Note: n-bit binary, the highest bit is the sign bit, so the numerical range represented is -2^(n-1) ——2^(n-1) -1, so the modulus is 2^(n-1).

2. Right shift operator

Right shift operator << causes all bits of the specified value to be shifted right by the specified number of times.
1) Its general format is as follows:
value >> num
num specifies the number of bits to shift the value value.
Just remember one thing about the rules of right shift: the sign bit remains unchanged, and the sign bit is added to the left

2) Operation rules:

Move all numbers to the right in binary form by the corresponding number of digits, shift out the low bits (discard), and high bits The empty bits are filled with sign bits, that is, positive numbers are filled with zeros, and negative numbers are filled with 1. When the operands of the right shift are byte and short types, these types will be automatically expanded to int type.
For example, if the value to be removed is a negative number, 1 is added to the left for each right shift. If the value to be removed is a positive number, 0 is added to the left for each right shift. This is called sign bit extension (sign preservation). bit) (sign extension), used to maintain the sign of negative numbers when performing a right shift

operation.

3) Mathematical meaning

Shifting one bit to the right is equivalent to dividing by 2, and shifting to the right by n bits is equivalent to dividing by 2 raised to the nth power.

4) Calculation process

11 >>2 (11 is int type)

1) The binary form of 11 is: 0000 0000 0000 0000 0000 0000 0000 1011
2) Move out the last two digits of the low digits, because the The number is positive, so zeros are padded in the high bits.
3) The final result is 0000 0000 0000 0000 0000 0000 0000 0010.
Converted to decimal is 3.

35 >> 2 (35 is int type)

35 converted to binary: 0000 0000 0000 0000 0000 0000 0010 0011

Move out the last two digits of the low bits: 0000 0000 0000 0000 0000 0000 0000 1000
Convert to decimal : 8

5) The sign is not retained when shifting right

The value after right shifting is bitwise ANDed with 0x0f, so that any sign bit extension can be discarded, so that the obtained value can be used as the subscript of the defined array, Thus, the hexadecimal character represented by the corresponding array element is obtained.

For example
public class HexByte {
public static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5 ', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', ​​'f''
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" hex[(b >> 4) & 0x0f] hex[b & 0x0f]);}}

(b >> 4) The operation process of & 0x0f: The binary form of

b is: 1111 0001

The 4-digit number is shifted out: 0000 1111
Bitwise AND operation: 0000 1111
Converted to the decimal form: 15
The operation process of

b & 0x0f: The binary form of
b is: 1111 0001
The binary form of 0x0f is: 0000 1111
Bitwise AND operation: 0000 0001
Converted to the decimal form: 1

So, this program The output of Specifies the number of bits to shift the value value.
Just remember one thing about the rules of unsigned right shift: ignore the sign bit extension and fill the highest bit with 0

The unsigned right shift operator>>> only makes sense for 32-bit and 64-bit values ​​








For more detailed explanations of Java shift operators and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template