I was messing around and noticed differences that I don't understand between Java and Perl when I bit-shifted -1.
I thought integers are stored in two's complement binary, so if there are 32 bits, -1 is 11...11 (32 times).
I'd expect -1 >> 1 to give $2^31 - 1$ = 011...11 = 2147483647.
I'd expect -1 << 1 to give = 11...110 = -2.
What is the reason for these different behaviors and where are these standards stated in different languages?
Code and printouts for Perl & Java below:
In Perl:
print (-1 >> 1, "\n");
print (-1 << 1, "\n");
2147483647
4294967294
In Java:
public class Tempy {
public static void main(String[] args){
System.out.println( -1 >> 1);
System.out.println( -1 << 1);
}
}
-1
-2
Perl's bit shift is inherently unsigned so -1 is treated as 2^32 -1 and it automatically fills with 0 so -1 >> 1 is 2^31-1 and -1 << 1 is 2^32-2.
[Edit] Thanks @Powerlord using integer will force perl to use signed values.
Java's bit shift sign extends (if using >>) so -1 << 1 is still -1 and -1 >> 1 is -2. If you don't want to sign extend you have to use the logical version >>>.
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