[bigint] Retrieving the N most significant bits?

Matt McCutchen <matt at mattmccutchen.net>
Thu Apr 17 10:43:28 PDT 2008


On Thu, 2008-04-17 at 11:22 -0500, Stuart White wrote:
> I'm porting some Java code to C++.  The Java code uses
> java.math.BigInteger, and I'm looking at using your C++ BigInteger
> class.
> 
> java.math.BigInteger has a method named bitLength() which returns the
> number of bits in the number.  I can use this method to retrieve the
> 32 most significant bits:
> 
>         BigInteger r = blah();
>         BigInteger f = r.shiftRight(r.bitLength() - 32);
>         System.out.println(f.toString());
> 
> I'm trying to accomplish this same functionality using your library,
> but nothing's jumping out at me.  Do you have a recommendation for how
> to retrieve the N most significant bits?  (BTW, I'm working only with
> unsigned integers, so I'm using BigUnsigned, in case that matters.)

You can implement your own bitLength function: determine the bit-length
of the most significant Blk of the number by testing one bit at a time
and then add the appropriate number of bits for the less-significant
Blks.  Then you can retrieve the desired number of most significant bits
the same way as before.

The bitLength function looks generally useful, so I will add it to the
library when I get a chance.

Matt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://mattmccutchen.net/mailman/archives/bigint/attachments/20080417/1183befa/attachment.pgp>


More information about the bigint mailing list