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

# 461. Hamming Distance

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LekNH5IywF8mjBxFcnu%2Fuploads%2FfTCNTKNvTeYbRdWUNvwo%2F%E5%9C%96%E7%89%87.png?alt=media\&token=f3be75a8-26da-47b6-a58d-fc0429872966)

time: O(k), k is the length of binary string of x^y

space: O(1)

```java
class Solution {
    public int hammingDistance(int x, int y) {
        int n = x^y;
        int count = 0;
        while (n != 0) {
            count++;
            n &= (n-1);
        }
        return count;
    }
}
```
