Reputation: 1199
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.
Upvotes: 9
Views: 875
Reputation: 9330
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);
Upvotes: 1
Reputation: 240482
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.
Upvotes: 10