Skip to content

Hamming Distance

EasyBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

The Hamming distance between two integers is the number of bit positions where their bits are different. Given x and y, return it.

x = 1, y = 4  →  2    (001 vs 100: positions 0 and 2 differ)
x = 3, y = 1  →  1    (11 vs 01)
x = 5, y = 5  →  0

Constraints

  • 0 <= x, y <= 2^31 - 1