find position of most significant bit ?

David T. Ashley dtashley@esrg.org
Tue, 26 Nov 2002 16:25:18 -0500


I assume one knows from the data structure of an arbitrary integer where the
limb containing the most significant bit is.

Then the problem reduces to the problem of finding the most significant
within a limb (a machine word).

I would tend to hardcode and bsearch it, i.e.

if (word & 0x0000FFFF)
   {
   if (word & 0x000000FF)
      {
      }
   else
      {
      }
   }
else
   {
   if (word & 0x00FF0000)
      {
      }
   else
      {
      }
   }

You could just roll and test, but I'm not sure how that would compare
against a hardcoded BSEARCH.

Dave.

> -----Original Message-----
> From: gmp-discuss-admin@swox.com [mailto:gmp-discuss-admin@swox.com]On
> Behalf Of Torbjorn Granlund
> Sent: Tuesday, November 26, 2002 1:04 PM
> To: George Coulouris
> Cc: gmp-discuss@swox.com
> Subject: Re: find position of most significant bit ?
>
>
> George Coulouris <coulouri@ncbi.nlm.nih.gov> writes:
>
>   mpz_sizeinbase(op, 2) would do it, but I don't know if there is a more
>   efficent way to accomplish this.
>
> mpz_sizeinbase(op,2) should need just a few cycles.
> Is that not efficient enough?  ;-)
>
> --
> Torbjörn
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss@swox.com
> https://gmplib.org/mailman/listinfo/gmp-discuss
>