Given two integers x
, and y
return the number of positions where their values differ in their binary representations as a 32-bit integer.
Example 1
Input
x = 9
y = 5
Output
2
9
in binary is 1001
and 5
in binary is 0101
, so indices 2 and 3 are different.
class Solution:
def solve(self, x, y):
ans = 0
while x or y:
ans += (x & 1) ^ (y & 1) x >>= 1 y >>= 1
return ans