Calculate hamming distance between two integers
Given two integers, return the Hamming distance between the two. The Hamming distance is the total number of places where the corresponding bits are different in the binary representation of two integers.
For example,
Output: 3
Explanation: The binary representation of 2 is 00000010, and 5 is 00000101. The first 3 bits are different at the far right of the bit string.
Input: x = 3, y = 9
Output: 2
Explanation: The binary representation of 3 is 00000011, and that of 9 is 00001001. The bits at the far right of the bit string are different in the 2nd and 4th positions.
We can easily solve this problem by taking the XOR of both integers. Since XOR cancels the same bits (1^1=0 and 0^0=0), it will result in an integer whose corresponding bits are different. Then the problem is reduced to counting the number of set bits in the resulting binary representation. There are several ways to achieve that:
1. Brute-Force Solution
A simple solution is to check each bit of the integer (say n) and keep track of the set bit count. We can use the expression n & 1 to get 1 or 0 depending on whether the last bit of n is set or unset and the right shift operator (n >>= 1) to discard the least significant bit (LSB). Note that this will require one iteration per bit. Therefore, it loops through 32 times for a 32–bit integer.
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namespace std; int findHammingDistance(int x, int y) { int count = 0; int n = x ^ y; for (int i = 0; i < 32; i++) { if (n & 1) { count++; } n >>= 1; } return count; } int main() { int x = 2, y = 5; cout << findHammingDistance(x, y) << endl; return 0; } |
Output:
3
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Main { public static int findHammingDistance(int x, int y) { int count = 0; int n = x ^ y; for (int i = 0; i < 32; i++) { if ((n & 1) != 0) { count++; } n >>= 1; } return count; } public static void main(String[] args) { int x = 2, y = 5; System.out.println(findHammingDistance(x, y)); } } |
Output:
3
Python
2. Using Brian Kernighan’s Algorithm
Alternatively, we can use Brian Kernighan’s bit counting algorithm to count set bits in an integer. The algorithm uses the expression n & (n-1) to flip the rightmost set bit of an integer n. Here, n-1 flips all the bits after the rightmost set bit of n, including the rightmost set bit.
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int findHammingDistance(int x, int y) { int count = 0; int n = x ^ y; while (n) { n = n & (n - 1); count++; } return count; } int main() { int x = 2, y = 5; cout << findHammingDistance(x, y) << endl; return 0; } |
Output:
3
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { public static int findHammingDistance(int x, int y) { int count = 0; int n = x ^ y; while (n != 0) { n = n & (n - 1); count++; } return count; } public static void main(String[] args) { int x = 2, y = 5; System.out.println(findHammingDistance(x, y)); } } |
Output:
3
3. Using Built-in functions
We may also use the built-in functions to return the number of set bits in the binary representation of the specified integer value. In GCC, you can use the __builtin_popcount() function to count the number of set bits. In Java, we have a similar method, Integer.bitCount(), for counting the set bits.
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int findHammingDistance(int x, int y) { return __builtin_popcount(x ^ y); } int main() { int x = 2, y = 5; cout << findHammingDistance(x, y) << endl; return 0; } |
Output:
3
Java
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)