给定一个正整数,输出它的补数,补数就是翻转原数二进制形式的每一位,如5(101)的补数为2(010)。
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
i = 1
while i <= num:
num ^= i
i <<= 1
return num
给定一个正整数,输出它的补数,补数就是翻转原数二进制形式的每一位,如5(101)的补数为2(010)。
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
i = 1
while i <= num:
num ^= i
i <<= 1
return num