Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there equivalent `int,short,long` in Perl?

Tags:

perl

64bits system:

$i=2;print ~$i; # 18446744073709551613

32bits system:

$i=2;print ~$i; # 4294967293

How can I make $i 32 bits?

I need a portable bitwise operation in Perl in any system.

like image 307
everbox Avatar asked Mar 07 '12 02:03

everbox


2 Answers

Just bitwise-and the result with 0xffffffff. This will have no effect on a 32-bit system, and give you the low-order 32 bits on a 64-bit system, which is the answer you want.

like image 159
hobbs Avatar answered Nov 15 '22 21:11

hobbs


For portable bitwise operations in Perl, check out the Bit::Vector library on CPAN.

It supports a wide range of bitwise operations, for example:

use Bit::Vector;

my $vector = Bit::Vector->new_Dec(32, "2"); # 32-bit vector for the decimal value 2
$vector->Negate($vector);
like image 30
sjs Avatar answered Nov 15 '22 19:11

sjs