http://www.perlmonks.org?node_id=163137


in reply to pack 64 bit int ?

I'd use left and right shift. I don't know how this would benchmark (and I don't have the time or energy to try ;) against your version, but this looks cleaner, at least to me.

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); }

Mine (as well as yours) will break if something unexpected is passed.

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:

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); }

This one looks even cleaner - and better yet, it works!