Re: Extracting bits out of huge numbers



Hi sisiphus,

I'll look at Math::GMP

In the doc I saw an interesting suggestion, which is to use
Math::BigInt, but let it use the faster libraries of Math::GMP.

This means probably, that I would run faste, but would stay backwards
compatible for the ones not having Math::GMP.

The suggested use statement is:

use Math::BigInt lib => 'GMP';

The function Math::GMP::gmp_tstbit() sounds very appealing to test a
few
bits of a big number.

However if I want to extract for example bits [967:13] , then the
approach to shift to the right and logical and then with a mask might
be
faster.
I have to try.


thanks and bye


H


On Jul 30, 4:12 am, sisyphus <sisyphus...@xxxxxxxxx> wrote:
On Jul 29, 10:28 pm, hofer <bla...@xxxxxxxxxx> wrote:



Hi,

I'd like to work with huge integers (> 64 bit precision)

Thus I can't use ordinary perl integers.

I thought Math::BigInt wouldn't be a too bad choice.

It's easy enough go create BigInts.
my $a = Math::BigInt->new("0x7777666655544443333222211110000");
my $b = Math::BigInt->new("0x1111111111111111111111111111111");

Calculating with them is also fine:
$a->badd($b); # $a = $a + $b

Now I would like to extract certain bits out of this huge number:

Example Bits 16 bis 12 should result in 0b00001 == 0x1 == 1
Bits 17 bis 12 should result in 0b100001 == 0x21 == 33

So far I see two ways of doing this conversion.

However I'm not really appealed by either solution.

Do you know anything faster / better or even another CPAN module?

Math::GMP will allow you to access individual bits. I would expect it
to be siginificantly faster than Math::BigInt, though I haven't done
any benchmarking.

use warnings;
use Math::GMP;

$x = Math::GMP->new('12344' x 7);

# print the 10 least siginificant bits:

print Math::GMP::gmp_tstbit($x, $_) for reverse(0..9);
print "\n";

Cheers,
Rob

.



Relevant Pages

  • Re: Count down on office trial
    ... You either ignored the warnings, or maybe you have a popup blocker that prevented the warnings from appearing. ... This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. ... If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. ...
    (microsoft.public.office.setup)
  • Re: Function without an As clause; return type of Object assumed
    ... My suggestion is to fix those warnings ... instead of disabling them. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Count down on office trial
    ... have a popup blocker that prevented the warnings from appearing. ... This post is a suggestion for Microsoft, ... Newsreader and then click "I Agree" in the message pane. ...
    (microsoft.public.office.setup)
  • Re: premature end of script headers
    ... use strict; ... use warnings; ... my ($suggestion); ... Posting guidelines Suggested Text me Suggested Text myself, ...
    (comp.lang.perl.misc)
  • Re: Cross tabulation - someone please help if you can
    ... Thanks for your suggestion. ... The trouble with this operation in java is that no warnings ...
    (comp.lang.java.help)