461 - Hamming Distance
Written on April 26, 2017
Tweet
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.
class Solution(object):
def hammingDistance(self, x, y):
xor = x ^ y
count = 0
for _ in xrange(32):
if xor & 1 == 1:
count += 1
xor >>= 1
return count