frankied has asked for the wisdom of the Perl Monks concerning the following question:
How would you pack a 64 bit int with a perl whose pack() doesn't support 'q' ? I came up with this:
...but surely there is a more elegant way ?sub pack64bit() { my $i=new Math::BigInt shift(); my $j=abs($i); my($int1,$int2)=(int($j/2**32)%2**32,int($j%2**32)); if ($i<0) { $int1^=-1; $int2^=-1; ++$int2; $int2%=2**32; $int1=($int1+1)%2**32 if $int2==0; } pack('NN',$int1,$int2); # or 'VV',$int2,$int1 }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: pack 64 bit int ?
by jsprat (Curate) on Apr 30, 2002 at 17:52 UTC | |
by Anonymous Monk on Apr 30, 2002 at 18:18 UTC | |
by frankied (Beadle) on Apr 30, 2002 at 18:23 UTC | |
(tye)Re: pack 64 bit int ?
by tye (Sage) on Apr 30, 2002 at 18:53 UTC | |
by frankied (Beadle) on May 01, 2002 at 21:05 UTC | |
Re: pack 64 bit int ?
by Elian (Parson) on May 01, 2002 at 15:14 UTC |
Back to
Seekers of Perl Wisdom