Python中正整数的位长 [英] Bit length of a positive integer in Python

查看:102
本文介绍了Python中正整数的位长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1 = 0b1 -> 1
5 = 0b101 -> 3
10 = 0b1010 -> 4
100 = 0b1100100 -> 7
1000 = 0b1111101000 -> 10
…

如何获取整数的位长,即在Python中表示正整数所需的位数?

How can I get the bit length of an integer, i.e. the number of bits that are necessary to represent a positive integer in Python?

推荐答案

在python 2.7+中,有一个

In python 2.7+ there is a int.bit_length() method:

>>> a = 100
>>> a.bit_length()
7

这篇关于Python中正整数的位长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆