Hamming Distance
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.
class Solution:
def hammingDistance(self, x, y):
Xor, ans = x ^ y, 0
while Xor:
ans += Xor & 1
Xor >>= 1
return ans
# other ways -
# return bin(Xor).count('1')
# return Xor.bit_count() # only since python 3.10
Posted by Jamie Meyer 25 days ago