Hamming Distance
Problem
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, return the Hamming distance between them.
Constraints
0 <= x, y <= 231 - 1
Solution
The problem Hamming Distance
can be solved using an XOR operator since it sets a bit only when given bits are different.
Implementation
class Solution
{
public:
int hammingDistance(int x, int y)
{
bitset<32> bit(x ^ y);
return bit.count();
}
};