千锋教育-做有情怀、有良心、有品质的职业教育机构
pythonint返回的方法探究

1、int额外的方法:
int.bit_length()
返回二进制表示整数所需的位数,不包括符号位和前面的零:
>>>n=-37
>>>bin(n)
'-0b100101'
>>>n.bit_length()
6
2、返回表示整数的字节组。
(1024).to_bytes(2,byteorder='big')
b'\x04\x00'
(1024).to_bytes(10,byteorder='big')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
(-1024).to_bytes(10,byteorder='big',signed=True)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
x=1000
x.to_bytes((x.bit_length()+7)//8,byteorder='little')
b'\xe8\x03'
以上就是pythonint返回的方法探究,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。
上一篇
python浮点数占多少个字节
相关推荐