How do you get bit_length() in PARI/GP

144 Views Asked by At

I've looked on google and couldn't find the answer and thought i'd ask PARI/GP users here. My question is simply How do you get bit_length() in PARI/GP, that you can use at there interpreter here: https://pari.math.u-bordeaux.fr/gp.html

3

There are 3 best solutions below

0
Piotr Semenov On BEST ANSWER

For integers, it is just bit_length(n) = #binary(n);. For example:

bit_length(n) = #binary(n);
bit_length(100)
> 7
0
Andrew On

Faster for large numbers than binary is just to use logint (requires n > 0).

1 + logint(n,2)
0
Charles On

Probably the fastest way (for positive integers) is

len(n)=exponent(n)+1