Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Hamming Distance in Python
The Hamming distance between two integers is the number of positions where the corresponding bits differ. For example, if we have numbers 7 and 15, they are 0111 and 1111 in binary respectively. Only the most significant bit differs, so the Hamming distance is 1.
Algorithm Steps
To calculate the Hamming distance, we follow these steps ?
- Iterate through each bit position from 31 down to 0
- Extract the bit at position i for both numbers using right shift and bitwise AND
- Compare the bits ? if they differ, increment the distance counter
- Return the total count of differing bits
Method 1: Using Bit Manipulation
This approach checks each bit position individually ?
class Solution:
def hammingDistance(self, x, y):
ans = 0
for i in range(31, -1, -1):
b1 = x >> i & 1
b2 = y >> i & 1
ans += not(b1 == b2)
return ans
# Test the function
solution = Solution()
result = solution.hammingDistance(7, 15)
print(f"Hamming distance between 7 and 15: {result}")
Hamming distance between 7 and 15: 1
Method 2: Using XOR Operation
A more efficient approach uses the XOR operation, which naturally highlights differing bits ?
def hamming_distance_xor(x, y):
# XOR gives 1 where bits differ
xor_result = x ^ y
# Count the number of 1s in the XOR result
count = 0
while xor_result:
count += xor_result & 1
xor_result >>= 1
return count
# Test with the same example
result = hamming_distance_xor(7, 15)
print(f"Hamming distance between 7 and 15: {result}")
# Test with more examples
print(f"Hamming distance between 1 and 4: {hamming_distance_xor(1, 4)}")
print(f"Hamming distance between 3 and 1: {hamming_distance_xor(3, 1)}")
Hamming distance between 7 and 15: 1 Hamming distance between 1 and 4: 2 Hamming distance between 3 and 1: 1
Method 3: Using Built-in bin() Function
For educational purposes, here's a more readable approach using binary string representation ?
def hamming_distance_string(x, y):
# Convert to binary and remove '0b' prefix
bin_x = bin(x)[2:]
bin_y = bin(y)[2:]
# Make both strings equal length by padding with zeros
max_len = max(len(bin_x), len(bin_y))
bin_x = bin_x.zfill(max_len)
bin_y = bin_y.zfill(max_len)
# Count differing positions
distance = sum(bit_x != bit_y for bit_x, bit_y in zip(bin_x, bin_y))
return distance
# Test the function
result = hamming_distance_string(7, 15)
print(f"Hamming distance between 7 and 15: {result}")
# Show binary representations
print(f"7 in binary: {bin(7)[2:].zfill(4)}")
print(f"15 in binary: {bin(15)[2:].zfill(4)}")
Hamming distance between 7 and 15: 1 7 in binary: 0111 15 in binary: 1111
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Bit Manipulation | O(log n) | O(1) | Standard implementation |
| XOR Operation | O(log n) | O(1) | Most efficient |
| String Comparison | O(log n) | O(log n) | Educational/debugging |
Conclusion
The XOR method is the most efficient for calculating Hamming distance, as it directly identifies differing bits. The bit manipulation approach provides good understanding of the underlying logic, while the string method offers clarity for learning purposes.
