Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR operation with "use integer" gives different values on Windows and Linux

Why am I getting different results for the XOR expression 0 ^ 2506133561 under the use integer pragma on Windows and Linux?

Windows:

perl -e "use integer; print 0^2506133561"
-1788833735

Linux:

perl -e 'use integer; print 0^2506133561'
2506133561
like image 723
Saravanan Avatar asked Jan 28 '26 11:01

Saravanan


1 Answers

Your Perl interpreter on Windows very likely uses 32-bit integers, while the one you're using on Linux has 64-bit ints.

To test this, run the shell command:

perl -V:ivsize

on each system. It should print ivsize='4' on 32-bit perls, and ivsize='8' on 64-bit perls. You can also obtain this information in a Perl script using $Config{ivsize} from the Config module.

To force the result of a bit operation to be interpreted as a signed 32-bit number even on 64-bit perls, you can use pack:

$num = unpack "l", pack "l", $num;

Alternatively, you can use simple bit operations and arithmetic like mpapec suggests:

$num &= 0xFFFFFFFF;
$num -= 2**32 if $num >= 2**31;
like image 148
Ilmari Karonen Avatar answered Jan 31 '26 07:01

Ilmari Karonen