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

2 answers 291 views
1 answer 204 views
1 answer 197 views
2 answers 270 views
...