Bit length of a positive integer in Python

后端 未结 7 1959
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 05:47
1 = 0b1 -> 1
5 = 0b101 -> 3
10 = 0b1010 -> 4
100 = 0b1100100 -> 7
1000 = 0b1111101000 -> 10
…

How can I get the bit length of an int

相关标签:
7条回答
  • 2020-12-13 06:46

    Here is another way:

    def number_of_bits(n):
        return len('{:b}'.format(n))
    

    Not so efficient I suppose, but doesn't show up in any of the previous answers...

    0 讨论(0)
提交回复
热议问题