How to find the number of bits necessary to represent an integer in binary with Python

1 Answer

0 votes
# bit_length() - Return the number of bits necessary to represent an integer in binary, 
#                excluding the sign and leading zeros:

n = -58

print(bin(n))

print(n.bit_length())


'''
run:

-0b111010
6

'''

 



answered Aug 11, 2024 by avibootz

Related questions

1 answer 178 views
2 answers 279 views
1 answer 189 views
2 answers 252 views
...