当前位置: 首页 > 图灵资讯 > 行业资讯> python int返回的方法探究

python int返回的方法探究

发布时间:2024-08-04 17:41:45

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'x000\x00\x00\x00\x00\x00\x00\x00\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)/8byteorder='little')
b'xe8\x03#39;

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

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

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12