Hamming Distance
Count the bit positions where two integers differ, with XOR and a loop that clears one 1 bit at a time.
Write two integers xyxy
Example 1
Output: 2
Explanation: 1 is 0001 and 4 is 0100. They differ in two positions.
Example 2
Output: 1
Explanation: 3 is 0011 and 1 is 0001. They differ in one position.
The Idea
Picture two rows of light switches, each switch either off (0) or on (1). The Hamming distance is how many switches you would have to flip to make one row look like the other. A number in binary is such a row: it uses only the digits 0 and 1, and each digit is called a bit.
Take Example 1. In binary, 1 is 00010100
Compare the rows labelled x = 1y = 4xy
A program cannot glance at two rows the way we just did. It could compare them one position at a time, but is there a way to mark every differing position at once?
Java's ^x ^ y = 50101
So the Hamming distance of xyx ^ y
Solution Approach
What is left is counting the 1 bits in x ^ y
Checking every position
How would we count the 1 bits by hand? Store the result of x ^ ydiff(diff >> i) & 1i>>& 1intdiff0101
Clearing the lowest 1 bit
Can the count skip the 0 bits? It can, because diff & (diff - 1)diff&
For diff = 0101
| diff before | diff - 1 | diff after | 1 bits cleared |
|---|---|---|---|
0101 | 0100 | 0100 | 1 |
0100 | 0011 | 0000 | 2 |
Each step removes exactly one 1 bit, so the number of steps until diff
Negative numbers
What if xyintdiffdiff
Optional: Java's built-in count
Java can also count the 1 bits for us. Integer.bitCount(x ^ y)
Java Solution
The method hammingDistance
- First, XOR the two numbers with
and keep the result inx ^ y . Each 1 bit indiff marks a position wherediff andx differ.y - Start a counter,
, at 0.count - Loop while
is not 0. Each pass clears the lowest 1 bit withdiff and adds 1 todiff &= diff - 1 .count - When
reaches 0, every 1 bit has been cleared once, sodiff is the Hamming distance, and the method returns it.count
A helper method, showInteger.bitCount(x ^ y)mainint
public class HammingDistance {
public static int hammingDistance(int x, int y) {
int diff = x ^ y; // 1 exactly where the bits differ
int count = 0;
while (diff != 0) {
diff &= diff - 1; // clear the lowest 1 bit
count++;
}
return count;
}
static void show(int x, int y) {
System.out.println("x = " + x + ", y = " + y + ": " + hammingDistance(x, y)
+ " (bitCount says " + Integer.bitCount(x ^ y) + ")");
}
public static void main(String[] args) {
show(1, 4);
show(3, 1);
show(7, 7); // equal numbers
show(0, -1); // -1 has all 32 bits set
show(Integer.MAX_VALUE, Integer.MIN_VALUE); // every bit differs
System.out.println(Integer.toBinaryString(-1));
}
}
Running it prints:
x = 1, y = 4: 2 (bitCount says 2)
x = 3, y = 1: 1 (bitCount says 1)
x = 7, y = 7: 0 (bitCount says 0)
x = 0, y = -1: 32 (bitCount says 32)
x = 2147483647, y = -2147483648: 32 (bitCount says 32)
11111111111111111111111111111111
The pairs 1, 4 and 3, 1 come out 2 and 1, as in the examples, and Integer.bitCountdiff
Look at the last line, -1 written in binary: 32 ones. That is why 0 and -1 are 32 apart. The largest int
Both of those pairs have a 1 at position 31, the leftmost, in diffdiffdiff != 0while (diff > 0)
Complexity
The work is one XOR, plus one pass of the loop for each 1 bit in diff
- Time: O(1), because the loop runs once per position where
andx differ, and a 32-bity has at most 32 such positions: 2 passes for 1 and 4, 32 for 0 and -1.int - Space: O(1), because the method keeps only
anddiff , whatever the two numbers are.count
The same counting extends to every pair in a list of numbers in