Learner Level
Implement the hammingDistanceValues method that returns the number of bit positions where two integers differ.
The input contains two integers, x and y.
Your task is to return the Hamming distance between them. The Hamming distance is the number of bit positions where the binary representations of the two numbers are different.
For example, 1 is binary 001 and 4 is binary 100. They differ in two positions, so the answer is 2.
Use XOR to find the positions where the bits are different.
For each bit position, XOR gives 1 when the bits are different and 0 when they are the same. So the problem becomes counting how many set bits are present in x XOR y.
Count the set bits by repeatedly checking the last bit and shifting the value to the right.
Pseudocode:
function hammingDistanceValues(x, y):
value = x XOR y
count = 0
while value > 0:
if value % 2 == 1:
count++
value = value / 2
return count