note
jsprat
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.
<p><code>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);
}
</code></p>Mine (as well as yours) will break if something unexpected is passed.
<p><b>Update:</b>Anonymous Monk, otherwise known as [frankied] noticed that this breaks for negative numbers.</p><p>Something bothered me anyway about using Math::BigInt to manipulate bits, so I did a rewrite using [cpan://Bit::Vector]:</p><p><code>
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);
}</code></p>This one looks even cleaner - and better yet, it works!
163123
163123