您现在的位置是:课程教程文章

python int返回的方法探究

2023-12-14 21:26课程教程文章 人已围观

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'

以上就是python int返回的方法探究,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

课程教程:python int返回的方法探究

上一篇:python indent如何打印JSON数据

下一篇:没有了

站点信息

  • 文章统计篇文章