# C#LeetCode刷题之#461-汉明距离​​​​​​​（Hamming Distance）

1   (0 0 0 1)
4  (0 1 0 0)
↑   ↑

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 < 231.

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.

```public class Program {

public static void Main(string[] args) {
var a = 16;
var b = 13;

var res = GetSum(a, b);
Console.WriteLine(res);

a = 168;
b = 136;

res = GetSum2(a, b);
Console.WriteLine(res);

}

public static int GetSum(int a, int b) {
//按位取异或
int result = a ^ b;
//判断是否需要进位
int forward = (a & b) << 1;
if(forward != 0) {
//如有进位，则将二进制数左移一位，进行递归
return GetSum(result, forward);
}
return result;
}

public static int GetSum2(int a, int b) {
while(b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}

}```

```True
False```

(2)