自Python3.1中,整数bit_length方法允许查询二进制的位数或长度。
常规做法:
>>> bin(256)
'0b100000000'
>>> len(bin(256)) - 2
9
>>>
使用函数:
>>> bin(256), (256).bit_length()
('0b100000000', 9)
>>> X = 99
>>> bin(X), X.bit_length()
('0b1100011', 7)
>>>
自Python3.1中,整数bit_length方法允许查询二进制的位数或长度。
>>> bin(256)
'0b100000000'
>>> len(bin(256)) - 2
9
>>>
>>> bin(256), (256).bit_length()
('0b100000000', 9)
>>> X = 99
>>> bin(X), X.bit_length()
('0b1100011', 7)
>>>