461. Hamming Distance ¶ Time: $O(32)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public: int hammingDistance(int x, int y) { int ans = 0; while (x > 0 || y > 0) { ans += (x & 1) ^ (y & 1); x >>= 1; y >>= 1; } return ans; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public int hammingDistance(int x, int y) { int ans = 0; while (x > 0 || y > 0) { ans += (x & 1) ^ (y & 1); x >>= 1; y >>= 1; } return ans; } } 1 2 3 4 5 6 7 8 9 10class Solution: def hammingDistance(self, x: int, y: int) -> int: ans = 0 while x > 0 or y > 0: ans += (x & 1) ^ (y & 1) x >>= 1 y >>= 1 return ans