Why is XOR often used in java hashCode (), but other bitwise operators are rarely used?

I often see code like

int hashCode(){ return a^b; } 

Why XOR?

+41
hashcode hash xor
Feb 25 '10 at 13:21
source share
4 answers

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

This truth table explains why:

 AB AND 0 0 0 0 1 0 1 0 0 1 1 1 AB OR 0 0 0 0 1 1 1 0 1 1 1 1 AB XOR 0 0 0 0 1 1 1 0 1 1 1 0 

As you can see, AND and OR do poor work when mixing bits.

OR will produce an average of 3/4 single-bit. And on the other hand will produce an average of 3/4 zero bits. Only XOR has an even one-bit allocation compared to the zero bit. This makes it so valuable for hash code generation.

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

+79
Feb 25 '10 at 13:27
source share

XOR has the following advantages:

  • It does not depend on the order of calculations, i.e. a ^ b = b ^ a
  • This is not "waste." If you change one bit in one of the components, the final value will change.
  • It is fast, one cycle even on the most primitive computer.
  • Keeps even distribution. If the two parts that you combine are evenly distributed, so there will be a combination. In other words, he is not inclined to collapse the digest range into a narrower band.

More details here .

+15
Feb 25 '10 at 13:32
source share

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

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

Now I can set the 1st row with the result to get the second row. i.e.

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

so that makes the second line the key. This behavior does not occur with another bit operator.

Please see this for more information β†’ Why is XOR used for cryptography?

+4
Feb 25 '10 at 13:32
source share

There is another use case: objects in which (some) fields should be compared without regard to their order . For example, if you want the pair (a, b) always be equal to the pair (b, a) .
XOR has the property that a ^ b = b ^ a , so it can be used in a hash function in such cases.

Examples: (full code here )

definition:

 final class Connection { public final int A; public final int B; // some code omitted @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Connection that = (Connection) o; return (A == that.A && B == that.B || A == that.B && B == that.A); } @Override public int hashCode() { return A ^ B; } // some code omitted } 

using:

  HashSet<Connection> s = new HashSet<>(); s.add(new Connection(1, 3)); s.add(new Connection(2, 3)); s.add(new Connection(3, 2)); s.add(new Connection(1, 3)); s.add(new Connection(2, 1)); s.remove(new Connection(1, 2)); for (Connection x : s) { System.out.println(x); } // output: // Connection{A=2, B=3} // Connection{A=1, B=3} 
+1
Mar 23 '14 at 13:25
source share



All Articles