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