Hamming Distance

Easy

Topics
Bit Manipulation

The Hamming distance between two integers is the number of positions at which the corresponding bits differ. Given integers a and b, return their Hamming distance.

Example 1

Input:  a = 1, b = 4
Output: 2
Explanation: 0001 vs 0100 differ in two bits.

Example 2

Input:  a = 3, b = 1
Output: 1

Constraints

  • 0 <= a, b <= 2^31 - 1
Run ⌘' · Submit ⌘⏎