Suppose we have two numbers x and y. We have to check whether these two numbers differ at one-bit position or not.
So, if the input is like x = 25 y = 17, then the output will be True because x = 11001 in binary and y = 10001. Only one-bit position is different.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def bit_count(n): count = 0 while n: count += n & 1 n >>= 1 return count def solve(x, y): return bit_count(x ^ y) == 1 x = 25 y = 17 print(solve(x, y))