文章目录
python 整数的二进制中只保留最右侧的1
def func1(n):
return n & -n
if __name__ == '__main__':
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
'''
list(map(bin, lst)):
['0b1', '0b10', '0b11', '0b100', '0b101', '0b110', '0b111', '0b1000', '0b1001', '0b1010']
'''
result = [1, 2, 1, 4, 1, 2, 1, 8, 1, 2]
assert result == list(map(func1, lst)), "错咯!"
print("Test pass!")
python 判断整数的二进制中1的个数
#n > 0
def func2(n