how to get the thighest bit position in big integers?

mmgarvey at gmx.de mmgarvey at gmx.de
Fri Oct 10 08:47:55 EDT 2008


On Oct 8, 10:56 am, Mark Dickinson <dicki... at gmail.com> wrote:
>> I think that (0).numbits()==-1 is the natural solution.
>
> Can you clarify this?  Why is -1 the natural solution? I
> can see a case for 0, for -infinity (whatever that means),
> or for raising an exception, but I can't see why -1 would
> be at all useful or natural.

You are right. I misunderstood the definition of nbits(). I initially
had asked for a function get_highest_bin_num(), which is always one
off
nbits() as it seems. I correct my sentence to

  The choice get_highest_bin_num(0) == -1 is the natural one (in
  concerns of cryptography). This property is needed for the
  algorithms I listed above.

If get_highest_bit_num(n) == nbits(n) - 1 holds for all n >= 0 then
we
both agree.

I cite from MRAB's post:
> int.numbits should return the position of the highest set bit (>=0)
> or -1 if none found.
It seems that MRAB had the same misunderstanding.

Perhaps one should implement both: numbits() and get_highest_bit_num()
where
numbits(0) = 0 and get_highest_bit_num(0) raises an exception?



More information about the Python-list mailing list