Integer bit_length method allows you to query the number of bits required to represent a number's value in binary.

You can get the same result by subtracting 2 from the length of the bin string using the len built-in function to handle leading "0b".

X = 99 print( bin(X), X.bit_length(), len(bin(X)) - 2 ) print( bin(256), (256).bit_length(), len(bin(256)) - 2 )

- Integers
- Integer Precision between Python 3.X and Python 2.X
- Convert integers to and from other bases' digit strings
- Using integer to control while loop