Hamming Distance

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.

Note: This question is the same as 2220: Minimum Bit Flips to Convert Number.

Example 1
Inputx = 1, y = 4
Output2
The binary representations of 1 and 4 differ at two bit positions.
Example 2
Inputx = 3, y = 1
Output1
The binary representations of 3 and 1 differ at one bit position.

Constraints

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

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate