class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i  1) ^ num
Simple Python

@protein.graph after the while loop, i is one bit left than num, if we minus 1, i and num will have the same bit range which can be used to solve our problem


@protein.graph
assume: num = 10101010
if you have tool=11111111
answer = tool ^num
tool = one more bit number 1while i <= num: i = i << 1
this is to get the one more bit number