Skip to content
Decoder

Hamming Distance

Easy

Problem

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

Example 1

Input
x = 1, y = 4
Output
2
Why
001 vs 100: positions 0 and 2 differ

Example 2

Input
x = 3, y = 1
Output
1
Why
11 vs 01

Example 3

Input
x = 5, y = 5
Output
0

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.