Skip to content

Instantly share code, notes, and snippets.

@ranasaani
Last active May 13, 2019 18:43
Show Gist options
  • Save ranasaani/16d750a51bbe55296a650153bf42535d to your computer and use it in GitHub Desktop.
Save ranasaani/16d750a51bbe55296a650153bf42535d to your computer and use it in GitHub Desktop.
461. Hamming Distance
def hammingDistance(x, y):
xor = '{0:32b}'.format(x ^ y)
count = 0
for x in xor:
if x == "1":
count += 1
return count
print(hammingDistance(1, 4))
"""
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.
Note:
0 ≤ x, y < 231.
Example:
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.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment