Given an integer number and we have to find necessary bits to represent it in binary in python.
To find necessary bits to represent a number – we use "bit_length()" method of "int" class, it is called with an integer object and returns the total number of bits to require to store/represent an integer number in binary.
Note: If the value is 0, bit_length() method returns 0.
Example:
Input:
num = 67 #binary: 1000011
# function call
print(num.bit_length())
Output:
7
Python code to find bits to represent an integer number
Output
need an explanation for this answer? contact us directly to get an explanation for this answer