You are viewing a single comment's thread. Return to all comments →
def sumXor(n): """ Key insights: * x + n <==> x | n * x + n == n ^ x <==> x | n == n ^ x <==> x & n == 0 * So need to count the number of n's zero bits. * An x can be an comination of these zero bits. * The total number is then 2 to the power of number of zero bits. This is becuase if the n bit is 0 or 1 ANDing it with 0 is still 0, hence two possible case for each bit. """ count = 0 while n > 0: count += 1 - n & 1 n >>= 1 return pow(2, count)
Seems like cookies are disabled on this browser, please enable them to open this website
Sum vs XOR
You are viewing a single comment's thread. Return to all comments →
Python 3