tags:

views:

371

answers:

3

I am a perl newbie,

Can I simply use 64-bit arithmetic in Perl?

For example

$operand1 = 0xFFFFFFFFFFFF;   // 48 bit value
$operand2 = 0xFFFFFFFFFFFF;   // 48 bit value

$Result = $operand1 * $operand2;
  • I am basically looking for a replacement for the int64_t in perl.
  • Is there any way to mention, if the variable is signed or unsigned?
+4  A: 

Yes, Perl automatically handles large integer arithmetic for you. However, Perl does not offer a distinction between signed and unsigned types (there's no need, since there are not fixed bounds on large integer range).

The perlnumber manual page has more information about the different numeric formats supported by Perl.

Greg Hewgill
It's not clear what you mean by "distinction between signed and unsigned values"?
ysth
Right, I should have said "types" instead of "values". Fixed.
Greg Hewgill
+10  A: 

Yes, however you need to have Perl compiled with 64-bit support.

Alan Haggai Alavi
"use bigint" will ensure that it always works. (If you want absolute speed, you picked the wrong programming language.)
jrockway
Perl is often used in bioinformatics and other fields requiring high-performance. If your numbers are sure to fit within 64 bits, then this answer will create better performing code. Just make sure to use a recent version of Perl, and you may want to compile it yourself."64-bit support is now considered to be mature"http://dev.perl.org/perl5/news/2002/07/18/580ann/
Colin
Even with 64 bit support, you'd need a `use integer;` to avoid getting a (rounded) floating point result and instead get a (high-bits truncated) integer result.
ysth
+6  A: 

See bigint.

Sinan Ünür
+1 thank you, but I could not use functions like hex() etc ~~~
Alphaneo