> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/461.hamming_distance.md).

# 461.Hamming Distance

**461.Hamming Distance**

难度:Easy

> 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 给出两个整数 x 和 y，计算它们之间的汉明距离。

```
注意：
0 ≤ x, y < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

上面的箭头指出了对应二进制位不同的位置。
```

简单的位运算:

```
class Solution {
public:
    int hammingDistance(int x, int y) {
        int n=0;
        while(x || y)
        {
            if((x&1) ^ (y&1)) n++;
            x= x>>1;
            y= y>>1;
          //  cout<<(x&1)<<(y&1)<<n<<endl;
        }
        return n;
    }
};
```
