Monky Python has asked for the wisdom of the Perl Monks concerning the following question:

I want to split a 64 bit number into 32 bit parts. The following code does work but it's quite slow. Does anybody know a faster solution?
my $pval=Math::BigInt->new(10531788168169408); my ($l,$r) =$pval->brsft(32);

MP

Replies are listed 'Best First'.
Re: split 64 bit number
by tadman (Prior) on Aug 11, 2001 at 03:13 UTC
    It is unfortunate that Perl does not have native 64-bit support (i.e. "long long"), as many compilers are adding this.

    That being said, what you are doing is functional, but since the Math::BigInt library does not appear to be optimized that well, it will be slow. This may be because the library is intended to work on very big numbers, much larger than 64 bit.

    If speed were an issue, a quick bit of Inline::C would certainly help.
      That perl does not have "native 64-bit support" is not entirely true. Perl has this support, but it is a compile-time option, and is usually not enabled. But when this support is enabled this task can be done with:

      my $num = 10531788168169408; my($l,$r) = ($n>>32 & 0xFfffFfff, $n & 0xFfffFfff);

      You can tell if perl is compiled with 64-bit integer support with perl -v. It will say something like:

      This is perl, vX.Y.Z built for i686-linux-64int-ld
      

      or maybe:

      This is perl, vX.Y.Z built for sun4-solaris-thread-64all
      

      (Of course the part that's emphasized is all that's important there.)

Re: split 64 bit number
by John M. Dlugosz (Monsignor) on Aug 11, 2001 at 07:07 UTC
    I've dealt with this some. For representing 64-bit numbers to pass to the Win32 API functions that take them, I came up with a couple functions to use 8-byte binary buffers. I can't remember where I have that right now, though.

    Another trick is when the actual realistic range is somewhat less. Although the parameter says 64-bit file size or whatever, I know it will really be a lot less. A floating-point value will hold a 56 bit (I think) mantissa without loss, so I use those.

    —John