What is the Pythonic way (without using any external libraries) to check if an integer is small enough to fit in a 64 bit signed quantity?
Sorry if this question has been asked before!
Just check the size with the int.bit_length()
method:
if integer_value.bit_length() <= 63:
The method takes the absolute value, so you want to leave a bit for the sign:
>>> (-2 ** 63).bit_length()
64
>>> (2 ** 63).bit_length()
64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With