Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are XOR often used in java hashCode() but another bitwise operators are used rarely?

Tags:

hashcode

hash

xor

I often see code like

int hashCode(){
  return a^b;
}

Why XOR?

like image 745
Andrei N Avatar asked Feb 25 '10 13:02

Andrei N


People also ask

What is Bitwise XOR operator in Java?

Bitwise XOR (exclusive or) "^" is an operator in Java that provides the answer '1' if both of the bits in its operands are different, if both of the bits are same then the XOR operator gives the result '0'.

What is the advantage of hashCode in Java?

hashCode in Java helps the program to run faster. For example, comparing two objects by their hashcodes will give the result 20 times faster than comparing them using the equals() function. This is so because hash data structures like HashMaps, internally organize the elements in an array-based data structure.

Is hashCode always unique in Java?

Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc. An object can also be searched with this unique code. Returns: It returns an integer value which represents hashCode value for this Method.

What is XOR in coding?

XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1). XOR table: X.


3 Answers

Of all bit-operations XOR has the best bit shuffling properties.

This truth-table explains why:

A B AND 0 0  0 0 1  0 1 0  0 1 1  1  A B OR 0 0  0 0 1  1 1 0  1 1 1  1  A B XOR 0 0  0 0 1  1 1 0  1 1 1  0 

As you can see for AND and OR do a poor job at mixing bits.

OR will on average produce 3/4 one-bits. AND on the other hand will produce on average 3/4 null-bits. Only XOR has an even one-bit vs. null-bit distribution. That makes it so valuable for hash-code generation.

Remember that for a hash-code you want to use as much information of the key as possible and get a good distribution of hash-values. If you use AND or OR you'll get numbers that are biased towards either numbers with lots of zeros or numbers with lots of ones.

like image 177
Nils Pipenbrinck Avatar answered Sep 29 '22 19:09

Nils Pipenbrinck


XOR has the following advantages:

  • It does not depend on order of computation i.e. a^b = b^a
  • It does not "waste" bits. If you change even one bit in one of the components, the final value will change.
  • It is quick, a single cycle on even the most primitive computer.
  • It preserves uniform distribution. If the two pieces you combine are uniformly distributed so will the combination be. In other words, it does not tend to collapse the range of the digest into a narrower band.

More info here.

like image 36
dogbane Avatar answered Sep 29 '22 18:09

dogbane


XOR operator is reversible, i.e. suppose I have a bit string as 0 0 1 and I XOR it with another bit string 1 1 1, the the output is

0 xor 1 = 1
0     1 = 1
1     1 = 0

Now I can again xor the 1st string with the result to get the 2nd string. i.e.

0   1 = 1
0   1 = 1
1   0 = 1

So, that makes the 2nd string a key. This behavior is not found with other bit operator

Please see this for more info --> Why is XOR used on Cryptography?

like image 37
Bhaskar Avatar answered Sep 29 '22 18:09

Bhaskar