use bit addition
class Solution:
def bitwiseComplement(self, N: int) -> int:
# time O(lgN) space O(1)
# N + complement(N) = 11111 = X
x = 1
while N > x:
x = x *2 +1
return x - N
use ^
class Solution:
def bitwiseComplement(self, N: int) -> int:
# N ^ bitwisecomplement(N) = 1111 = X
# ===> N ^ X = bitwisecomplement(N)
# time O(lgN) space O(1)
x = 1
while N> x:
x = x*2 +1
return N^x