Package com. super119;
Public class negativeshift {
Public static void main (string [] ARGs ){
Byte B =-8;
/*
* Be caution:
* 1. byte will be auto promoted to int while shifting
* 2. Left shift has not sign-extended
* Right shift has sign-extended but it has ">>>" to remove sign-extended
* 3. negative value represented in "two complement" Way
*/
// Sign-extended, so we got "-4" here.
System. Out. println (B + "> 1 is:" + (B> 1 ));
// We got "-16" here.
System. Out. println (B + "<1 is:" + (B <1 ));
// Sign-extended canceled, so we got "2147483644" here.
System. Out. println (B + ">>> 1 is:" + (B >>> 1 ));
Int I =-0x7fffffff; // represents as "1000... 1" in memory
// Left shift has not sign-extended
System. Out. println (I + "<1 is:" + (I <1); // so we got a 2 here
// The reason for why left shift has not a "<" operator is,
// It will exceed the max value of int
}
}