## Shift operator in Java

Lawrence 2020-11-11 17:30:32
shift operator java

1、 Shift left operator ：<<

Let's define a random int Number of types int, Decimal value = 733183670, The conversion to binary is represented in the computer as follows ：

value << 1, Move left 1 position

Move left 1 The value converted to decimal is ：1466367340, just 733183670 Twice as many , Some people are riding 2 I like to use the left shift operator instead of .

value << 8, Move left 8 Let's have a look ：

Move left 8 The value that becomes decimal after bit is ：-1283541504, Move 8 Behind you , Because the first place became 1, That is to say, it becomes a negative number , In use, we should consider the case of negative numbers .

According to this rule , Move left 32 Behind you , On the right 32 individual 0 Does the value become decimal 0 了 ？ The answer is NO, When int When type moves left , Left shift is greater than or equal to 32 Bit operation , I will ask for the surplus first （%） Then move left . That is to say, shift left 32 Bit is equivalent to no shift operation , Move left 40 Bit is equivalent to a left shift 8 position （40%32=8）. When long When type moves left ,long The representation of type in binary is 64 Bit , So the cardinality of the remainder operation becomes 64, That is to say, shift left 64 Bit is equivalent to no shift , Move left 72 Bit is equivalent to a left shift 8 position （72%64=8）, Write a piece of code to test

``````int intValue = 733183670;// Write a random number
System.out.println("intValue：" + (intValue));// Print intValue System.out.println("intValue Move left 1 position ：" + (intValue << 1));// Move left 1 position System.out.println("intValue Move left 8 position ：" + (intValue << 8));// Move left 8 position // When int Type shift left is greater than or equal to 32 Bit operation , Will be the remainder before the shift operation
System.out.println("intValue Move left 32 position ：" + (intValue << 32));// The rest is 32%32=0, It's equivalent to moving left 0 position （ No displacement ）
System.out.println("intValue Move left 40 position ：" + (intValue << 40));// The rest is 40%32=8, It's equivalent to moving left 8 position
System.out.println("intValue Move left 64 position ：" + (intValue << 64));// The rest is 64%32=0, It's equivalent to moving left 0 position （ No displacement ）
long longValue = 733183670L;
System.out.println("longValue：" + (longValue));// Print longValue System.out.println("longValue Move left 1 position ：" + (longValue << 1));// Move left 1 position System.out.println("longValue Move left 8 position ：" + (longValue << 8));// Move left 8 position // When long Type shift left is greater than or equal to 64 Bit operation , Will be the remainder before the shift operation
System.out.println("longValue Move left 64 position ：" + (longValue << 64));// The rest is 64%64=0, It's equivalent to moving left 0 position （ No displacement ）
System.out.println("longValue Move left 72 position ：" + (longValue << 72));// The rest is 72%64=8, It's equivalent to moving left 8 position
System.out.println("longValue Move left 128 position ：" + (longValue << 128));// The rest is 128%64=0, It's equivalent to moving left 0 position （ No displacement ）``````

Take a look at the results ：

because double,float It's very special in binary , So we can't do the shift operation , Report errors , Compile but , Here's the picture ：

Be careful ： Other kinds of plastic surgery byte,short It will be converted to int type （32 position ） And then move it , There's no code testing here , If you are interested, you can test it yourself .

in summary ： Move left << It's very simple , in other words Discard the specified number of digits on the left , The right to repair 0.

2、 Shift right operator ：>>

It's still this number ：733183670

value >> 1, Move right 1 position

Move right 1 The value converted to decimal is ：366591835, just 733183670 Of 1 And a half , Some people are removing 2 You like to use the right shift operator instead of

value >> 8, Move right 8 Let's have a look

Write a code to test

``````
java
int intValue = 733183670;// Write a random number System.out.println("intValue：" + (intValue));// Print intValue System.out.println("intValue Move right 1 position ：" + (intValue >> 1));// Move right 1 position System.out.println("intValue Move right 8 position ：" + (intValue >> 8));// Move right 8 position // When int Type shift right is greater than or equal to 32 Bit operation , Will be the remainder before the shift operation
System.out.println("intValue Move right 32 position ：" + (intValue >> 32));// The rest is 32%32=0, It's equivalent to moving right 0 position （ No displacement ）
System.out.println("intValue Move right 40 position ：" + (intValue >> 40));// The rest is 40%32=8, It's equivalent to moving right 8 position
System.out.println("intValue Move right 64 position ：" + (intValue >> 64));// The rest is 64%32=0, It's equivalent to moving right 0 position （ No displacement ）
long longValue = 733183670L;
System.out.println("longValue：" + (longValue));// Print longValue System.out.println("longValue Move right 1 position ：" + (longValue >> 1));// Move right 1 position System.out.println("longValue Move right 8 position ：" + (longValue >> 8));// Move right 8 position // When long Type shift right is greater than or equal to 64 Bit operation , Will be the remainder before the shift operation
System.out.println("longValue Move right 64 position ：" + (longValue >> 64));// The rest is 64%64=0, It's equivalent to moving right 0 position （ No displacement ）
System.out.println("longValue Move right 72 position ：" + (longValue >> 72));// The rest is 72%64=8, It's equivalent to moving right 8 position
System.out.println("longValue Move right 128 position ：" + (longValue >> 128));// The rest is 128%64=0, It's equivalent to moving right 0 position （ No displacement ）
``````

result ：

It's the same as moving left ,int Type shift is greater than or equal to 32 When a ,long Type is greater than or equal to 64 When a , Will do residual processing first, then displacement processing ,byte,short It will be converted to int type （32 position ） And then move it . That's a positive displacement , Let's look at the right shift of negative numbers , Pictured , negative intValue：-733183670 The binary representation of is shown in the figure below ：

Move right 8 position ,intValue >> 8

in summary ： Shift right operator >> The rules of operation are also very simple , Discard the specified number of digits on the right , Fill in the sign on the left .

3、 Unsigned shift right operator ：>>>

Unsigned shift right operator >>> And the shift right operator >> It's the same , It's just that when you shift right, the left is filled with the sign bit , The unsigned right shift operator is a complement 0, in other words , For a positive shift, it is equivalent to ：>>, Negative numbers can be shifted to positive numbers by this shift operator . With -733183670>>>8 For example, draw a picture

Unsigned shift right operator >> The rules of operation are also very simple , Discard the specified number of digits on the right , Make up on the left 0.