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 < 2^{31}.

**Solution I:**

```
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
str1 = bin(x)[2:]
str2 = bin(y)[2:]
len1 = len(str1)
len2 = len(str2)
num = abs(len1 - len2)
if len1 < len2:
str1 = '0' * num + str1
elif len2 < len1:
str2 = '0' * num + str2
res = 0
for i in range(0, len(str1)):
if str1[i] != str2[i]:
res += 1
return res
slt = Solution()
print(slt.hammingDistance(x=3, y=4))
```

**Solution II:**

```
class Solution(object):
def hammingDistance(self, x, y):
return bin(x ^ y).count('1')
```

via: http://zhangzhenyu.com.cn/, all copyright by Mr.zzy.