Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(tye)Re: pack 64 bit int ?

by tye (Sage)
on Apr 30, 2002 at 18:53 UTC ( #163143=note: print w/replies, xml ) Need Help??


in reply to pack 64 bit int ?

I came up with this for unpacking 64-bit integers into Perl's native scalars.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: pack 64 bit int ?
by frankied (Beadle) on May 01, 2002 at 21:05 UTC
    (wondering how I missed that node when searching)

    I did this for unpack:

    my ($int1,$int2)=unpack('NN',shift()); my $sign=($int1&0x80000000); if($sign) { $int1^=-1; $int2^=-1; ++$int2; $int2%=2**32; ++$int1 unless $int2; } my $i=new Math::BigInt $int1; $i*=2**32; $i+=$int2; $i=-$i if $sign;
    your 2's complement conversion looks less painful, though.

    update:
    shamelessly (thank you!) stealing ideas from tye's unpack, I would now write down a pack like:

    my $i=new Math::BigInt shift(); my($int1,$int2)=do { if ($i<0) { $i=-1-$i; (~(int($i/2**32)%2**32),~int($i%2**32)); } else { (int($i/2**32)%2**32,int($i%2**32)); } }; pack('NN',$int1,$int2);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://163143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2021-02-27 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?