We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
For every un-set bit , there are 2 posiiblities of a number that when added to the original number would give the same answer , if xored
So , count the number of unset bits , and then raise it to the power of 2 , to get all combinations of numbers that when added to n will give the same result as to when xored by n
def sumXor(n):
# Write your code here
ct = 0
while n:
if n&1==0:
ct+=1
n>>=1
return 2**ct
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Sum vs XOR
You are viewing a single comment's thread. Return to all comments →
So , count the number of unset bits , and then raise it to the power of 2 , to get all combinations of numbers that when added to n will give the same result as to when xored by n
def sumXor(n): # Write your code here ct = 0 while n: if n&1==0: ct+=1 n>>=1 return 2**ct
`