```
class Solution:
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
mask = 1
# left shift the mask until it is greater or equal to the input
# append an 1 at each shift
# at the end we will have a mask which is "111111..." in binary
# it is not less than the input, which guarantees they have the same length in binary.
while mask < num:
mask = mask << 1
mask = mask | 1
# simply xor them, done! (10101 xor 11111 -> 01010)
return num ^ mask
```

** Maybe there is simpler way to generate the mask.