Hamming Distance

Easy~10 min

Given two non-negative integers x and y, return the Hamming distance between them.

The Hamming distance is the number of bit positions where the corresponding bits of the two integers differ.

For example, 1 (001) and 4 (100) differ in 2 positions.

Examples

Example 1
Input: x = 1, y = 4
Output: 2
Explanation: 1 = 001, 4 = 100. They differ at bit positions 0 and 2.
Example 2
Input: x = 3, y = 1
Output: 1
Explanation: 3 = 11, 1 = 01. They differ only at bit position 1.
Example 3
Input: x = 0, y = 0
Output: 0
Explanation: Both are 0, so no bits differ.

Constraints

  • 0 <= x, y <= 2^31 - 1
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run