All problems

Hamming Distance

Easy · bit manipulation

Every non-negative integer can be written in binary, as a row of 0 and 1 digits (bits). Given two non-negative integers x and y, count the bit positions where their binary forms differ.

Line the two numbers up on the right and treat the shorter one as if it had 0s written in front. For example, 1 is 001 and 4 is 100. They differ in the first and the last position, so the answer is 2.

Examples

Example 1

Input: x = 1, y = 4

Output: 2

001 and 100 differ in two positions: the leftmost and the rightmost.

Example 2

Input: x = 3, y = 1

Output: 1

11 and 01 differ only in the left position.

Example 3

Input: x = 7, y = 7

Output: 0

The numbers are equal, so no position differs.

Constraints