Here's a tiny java program
public class otherclass {
public static void main(String[]args){
byte a=-5;
byte d= (byte) (a>>>1);
System.out.println(d);
byte e= (byte) (a>>>2);
System.out.println(e);
byte f= (byte) (a>>1);
System.out.println(f);
byte g= (byte) (a>>2);
System.out.println(g);
}
}
output:
-3
-2
-3
-2
The second two outputs (those -3 and -2 of the logical shifts) I understand.
negative 5 is 11111011
arithmetic shift shifts to the right and the extra added bit on the left is like the MSB. So one shift makes 11111101
which is negative 3. Negative two is also fine.
The logical shift is supposed to add zeros to the left. 11111011
should become 01111101
which is 125. How does it also output negative 3?