[Solved] Xor logic in python


The trick is to recognize that you don’t have to test all the a up to x. For a^x == a+x, then a&x == 0. So we count the number of zeroes in the bitstring of x and then out answer is 2**count -1

test = int(input())
for _ in range(test):
    x = int(input())
    print(2**bin(x)[2:].count('0') -1)

0

solved Xor logic in python