The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, return the Hamming distance between them.
Example 1:
Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.
Example 2:
Input: x = 3, y = 1 Output: 1
Constraints:
0 <= x, y <= 231 - 1
Note: This question is the same as 2220: Minimum Bit Flips to Convert Number.
The Hamming distance between two integers is the number of positions at which the corresponding bits are different. This problem efficiently calculates this distance.
Core Idea:
The most efficient approach leverages the bitwise XOR operator (^
) and the built-in functions for counting set bits (1s).
XOR Operation: The XOR operation (^
) compares corresponding bits of two integers. If the bits are the same, the result is 0; if they are different, the result is 1. Therefore, x ^ y
produces an integer where each bit represents whether the corresponding bits in x
and y
are different.
Counting Set Bits: The number of set bits (1s) in the result of x ^ y
directly corresponds to the Hamming distance. Most languages provide optimized built-in functions for this:
bit_count()
methodInteger.bitCount()
method__builtin_popcount()
functionbits.OnesCount()
functionTime and Space Complexity:
Code Explanation (Python):
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return (x ^ y).bit_count()
This Python code directly uses the bit_count()
method to achieve the solution concisely. The XOR operation is performed first, and then the bit_count()
method efficiently counts the set bits.
Code Explanation (Java):
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
This Java code mirrors the Python solution, utilizing Integer.bitCount()
for efficient counting of set bits.
Code Explanation (C++):
class Solution {
public:
int hammingDistance(int x, int y) {
return __builtin_popcount(x ^ y);
}
};
The C++ code employs __builtin_popcount()
, a built-in function that counts the set bits in an integer.
Code Explanation (Go):
func hammingDistance(x int, y int) int {
return bits.OnesCount(uint(x ^ y))
}
The Go code uses bits.OnesCount()
, part of the math/bits
package, to count set bits after the XOR operation. Note the type conversion to uint
.
Code Explanation (TypeScript/JavaScript):
function hammingDistance(x: number, y: number): number {
x ^= y;
let ans = 0;
while (x) {
x -= x & -x;
++ans;
}
return ans;
}
This version doesn't rely on built-in functions to count set bits. It iteratively clears the least significant set bit using x -= x & -x
and increments the counter ans
until x
becomes 0. This is a slightly less efficient approach than using built-in functions but still demonstrates the core concept of counting differing bits. The time complexity remains O(1) because the number of iterations is limited by the number of bits in the integer.
All the provided solutions efficiently calculate the Hamming distance using bitwise operations and optimized built-in functions (where available), resulting in a constant time complexity solution. The alternative TypeScript/JavaScript solution provides a manual bit-counting approach, illustrating the underlying principle.