What is the equivalent of Python's int.bit_length()
in Julia?
int.bit_length()
: Return the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros
In Julia there is the ndigits
function.
ndigits(n::Integer; base::Integer=10, pad::Integer=1)
Compute the number of digits in integer n written in base base (base must not be in [-1, 0, 1]), optionally padded with zeros to a specified size (the result will never be less than pad).
Examples
julia> ndigits(12345) 5 julia> ndigits(1022, base=16) 3 julia> string(1022, base=16) "3fe" julia> ndigits(123, pad=5) 5
You want to use it with the base = 2
keyword argument:
julia> ndigits(32, base = 2)
6
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