in reply to pack 64 bit int ?
Mine (as well as yours) will break if something unexpected is passed.sub pack64bit { my $i = new Math::BigInt shift(); my $j = new Math::BigInt $i->brsft(32); my $k = $i - $j->blsft(32); return pack('NN', $j, $k); }
Update:Anonymous Monk, otherwise known as frankied noticed that this breaks for negative numbers.
Something bothered me anyway about using Math::BigInt to manipulate bits, so I did a rewrite using Bit::Vector:
This one looks even cleaner - and better yet, it works!sub pack_bv{ use Bit::Vector; my $vec = Bit::Vector->new_Dec(64, shift); return pack 'NN', $vec->Chunk_Read(32, 32), $vec->Chunk_Read(32, 0); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: pack 64 bit int ?
by Anonymous Monk on Apr 30, 2002 at 18:18 UTC | |
by frankied (Beadle) on Apr 30, 2002 at 18:23 UTC |