0

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

Andrew
  • 873
  • 4
  • 10
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24

3 Answers3

3

Probably the fastest way (for positive integers) is

len(n)=exponent(n)+1
Charles
  • 11,269
  • 13
  • 67
  • 105
2

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

bit_length(n) = #binary(n);
bit_length(100)
> 7
Piotr Semenov
  • 1,761
  • 16
  • 24
1

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

1 + logint(n,2)
Andrew
  • 873
  • 4
  • 10