当前位置:网站首页>h = key. Hashcode()) ^ (H > > 16) detailed explanation and why the hashcode value should be shifted to the right by 16 bits and XOR with the original hashcode value

h = key. Hashcode()) ^ (H > > 16) detailed explanation and why the hashcode value should be shifted to the right by 16 bits and XOR with the original hashcode value

2022-06-22 06:10:00 Xiaoshuang is handsome enough to drag the Internet speed

h = key.hashCode()) ^ (h >>> 16) Detailed interpretation

static final int hash(Object key) {
    
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

At present key = “java” Of hashCode() = 3254818 It's in decimal

 Insert picture description here

Decimal system :3254818
Be careful :int type Occupy 4byte 32 position

Processing data

Binary system :11 0001 1010 1010 0010 0010 (22 position , Insufficient 32 position , Then fill 0 To 32 position )

0000 0000 0011 0001 1010 1010 0010 0010 (32 position )

h>>>16 unsigned right shift 16 position

0000 0000 0000 0000 0000 0000 0011 0001

^ Exclusive or operation : Same as 0, The difference is 1, It can be understood as Add without carry

Data processing finished , Start XOR operation

h = key.hashCode() ^ (h >>> 16)

0000 0000 0011 0001 1010 1010 0010 0010 ( h = key.hashCode())

^ ( Exclusive or )

0000 0000 0000 0000 0000 0000 0011 0001 (h >>> 16)

The result is :

0000 0000 0011 0001 1010 1010 0001 0011

3245803 ( Decimal results )

 Insert picture description here

The calculated results are consistent with the results fed back by the program

 Insert picture description here

Many friends will ask after reading , Why would you hashCode Value shift right 16 Bit and with the original hashCode Value for ^( Bitwise XOR ) operation ?

  1. Move right 16 Bit is to make high 16 Bits are also involved , Better uniform hashing , Reduce collisions , Further reduction hash The odds of conflict
  2. The XOR operation is to better preserve the two groups 32 The respective characteristics of a binary number

If you are using &( Bitwise AND ) operation , Then the result will be :

It is obvious that 2 Hexadecimal result direction 0 close , Because the result of bitwise and has 4 Kind of , Only 1&1 The result is 1, The rest of the cases are 0, In this way, it is not only unable to retain the respective characteristics of the original two sets of data , It will also be significantly raised hash The odds of conflict

 Insert picture description here

If you are using |( Press bit or ) operation , Then the result will be :

It can be found that although the result of the same XOR is not very different , But it can also be found that the result is 1 close , And can not well retain their own characteristics

besides , The biggest difference between XOR and or is , XOR can only satisfy one condition , And or can satisfy both conditions at the same time

Exclusive or : Same as 0, The difference is 1 , If the result is 1, What may happen is 00 11 This situation can well retain their own characteristics , Because they are all the same 0 Good 1 Let it be

or : There is one for 1 Then for 1, All two are 0 Then for 0 , If the result is 1, What may happen is 10 11, In these cases, it is difficult for us to judge the characteristics of the two groups of data involved in the operation

Obviously bitwise OR (|) The operation can not well preserve the characteristics of two groups of numbers

 Insert picture description here

The above content is only for personal interpretation , If there is any misunderstanding , Welcome to comment , Thank you

原网站

版权声明
本文为[Xiaoshuang is handsome enough to drag the Internet speed]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220557452911.html