Python - Integer bit length

Introduction

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".

Demo

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

Result

Related Topic