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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With