Bill Allombert on Fri, 19 Oct 2012 18:26:13 +0200

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Word length and number of bits in an integer

On Fri, Oct 19, 2012 at 05:58:11PM +0200, Dirk Laurie wrote:
> I need a routine for the exact smallest value B such that x>>B is zero,
> where x is a t_INT.  SInce a may be very large indeed, I coded it thus:
> { bitsin(x) =
>    my(w=32*(length(x)-1)); x>>=w;
>       while(x, x>>=1; w+=1);
>    w
> }
> I have two queries:
> 1. Am I missing some useful builtin function?

Yes, but I do not thing it is available in GP.
you can do

Maybe we could add a function logint().

> 2. Can I rely on the words making up x to be 32-bit?
>     If not, how do I find out the word length from inside GP?

You should not try :)