The difference between >> and >>>

prologue

When looking HashMap source, see HashMap hash function which is useful to >>> operators, often prior to use in addition to the operation 2 >> operator, but still >>> first saw, so they come record it.

Scenario reproduction

hashMap hash function source

hashMap hash function source

Because it is the first major acquisition of hashCode key, which is generated jvm, so I'm alone with one simulation hashCode

System.out.println((h = 1) ^ (h >>> 16));

The results are as follows

情景复现: 1

Step resolve

This code consists of three sections of the code obtained by calculation

  1. h direct assignment of 1
  2. h>>>16位
  3. Steps 1 and 2 of the exclusive OR operation

Therefore, the results deduced from the result 0 16 1 >>>

test

System.out.println("-12>>>2结果为:"+Integer.toBinaryString(-12>>>2));
System.out.println("12>>>2结果为:"+Integer.toBinaryString(12>>>2));
System.out.println("-12>>2结果为:"+Integer.toBinaryString(-12>>2));
System.out.println("12>>2结果为:"+Integer.toBinaryString(12>>2));
-12>>>2结果为:111111111111111111111111111101
12>>>2结果为:11
-12>>2结果为:11111111111111111111111111111101
12>>2结果为:11

analysis

As can be seen from the results, when do the operation with a positive number, >> and >>> results did not change, but change is negative in operation, this comparison demonstrates the operation and the sign bit >>> concerned.

  • Positive operation

Due to bit operation, without using an output method of outputting front Integer.toBinaryString 0, it can be inferred that two operators are operating right by n bits 0s

  • Negative operation

Since the negative storage is its complement, when performing arithmetic apparent >>> generating a complement binary string, and the length of time than a positive operand longer, java in binary sign bit is not distinguished , so the final number of decimal representation will be unusually large.

to sum up

Two operators, the number of shifts when performing the forward operation is the same. However, when dealing with negative numbers, up >> 0, the operation is unsigned. >>> 1 and up, it is a signed operation.

    This article first appeared in the cartoon's blog
     please indicate the source: https://cartoonyu.github.io/cartoon-blog/post/java/%E6%97%A0%E7%AC%A6%E5%8F%B7%E8 % BF% 90% E7% AE % 97% E7% AC% A6% E4% B8% 8E% E6% 9C% 89% E7% AC% A6% E5% 8F% B7% E8% BF% 90% E7% AE % 97% E7% AC% A6 % E7% 9A% 84% E5% 8C% BA% E5% 88% AB /

Guess you like

Origin www.cnblogs.com/cartooon/p/11455481.html