H
hofer
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?
# extract bits out of binary string
sub extbits { #
my ($val,$msb,$lsb) = @_; # $val must be Math::BigInt;
my $asbinstr = $val->as_bin(); # nun als binaer string
my $withoutprefix = substr($asbinstr,2); # fuehrendes '0b'
entfernen
my $substr = substr($withoutprefix,-$msb-1,$msb-$lsb+1); # den
substring extrahieren
$substr = 0 if $substr eq ""; # sollte mindestens einen character
enthalten
my $result = Mat::BigInt->new("0b".$substr); # zurueck in
Math::BigInt verwandeln
return $result;
} #
# extract bits via shifts operations follwed by a bit wise and
sub extbits { #/*{{{*/
my ($val,$msb,$lsb) = @_; # $val must be Math::BigInt;
my $mask = Math::BigInt->new(1); # create a 1
$mask->blsft($msb-$lsb+1); # 2 ^ (number of bits to extract)
$mask->bsub(1); # now we have a mask
my $tmp = $val->copy->brsft($lsb); # shift input value to the
right
return $tmp->band($mask);
} #/*}}}*/
Thanks in advance for any other suggestions
like rewriting the function to accelerate it or using another module.
bye
H
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?
# extract bits out of binary string
sub extbits { #
my ($val,$msb,$lsb) = @_; # $val must be Math::BigInt;
my $asbinstr = $val->as_bin(); # nun als binaer string
my $withoutprefix = substr($asbinstr,2); # fuehrendes '0b'
entfernen
my $substr = substr($withoutprefix,-$msb-1,$msb-$lsb+1); # den
substring extrahieren
$substr = 0 if $substr eq ""; # sollte mindestens einen character
enthalten
my $result = Mat::BigInt->new("0b".$substr); # zurueck in
Math::BigInt verwandeln
return $result;
} #
# extract bits via shifts operations follwed by a bit wise and
sub extbits { #/*{{{*/
my ($val,$msb,$lsb) = @_; # $val must be Math::BigInt;
my $mask = Math::BigInt->new(1); # create a 1
$mask->blsft($msb-$lsb+1); # 2 ^ (number of bits to extract)
$mask->bsub(1); # now we have a mask
my $tmp = $val->copy->brsft($lsb); # shift input value to the
right
return $tmp->band($mask);
} #/*}}}*/
Thanks in advance for any other suggestions
like rewriting the function to accelerate it or using another module.
bye
H