思路:
x&1 求x的第一位,y&1 求y的第一位,进行比较如果不相同那么res+1
x >> 1, y >> 1 继续&1进行比较
func HammingDistance(x int, y int) int {
res := 0
for x != 0 || y != 0 {
if x&1 != y&1 {
res++
}
x >>= 1
y >>= 1
}
return res
}
思路:
x&1 求x的第一位,y&1 求y的第一位,进行比较如果不相同那么res+1
x >> 1, y >> 1 继续&1进行比较
func HammingDistance(x int, y int) int {
res := 0
for x != 0 || y != 0 {
if x&1 != y&1 {
res++
}
x >>= 1
y >>= 1
}
return res
}