hamming distance

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, calculate the Hamming distance.
Example:

1
2
3
4
5
6
7
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.

method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
x=3
y=4
s1 = []
l1 = []
n =0
while x != 0 or y !=0 :
s1.append(x % 2)
l1.append(y % 2)
x = x // 2
y = y // 2
s2 = list(reversed(s1))
l2 = list(reversed(l1))
for i in range(0,len(s1)):
if s2[i] == l2[i]:
n=n
else :
n+=1
print(n)