class shifting{ public static void main (String args[]) { byte b = -13; //11110011 b >>>= 1; //01111001 System.out.println(b); } } I get the answer as 121 But the correct answer is b= -7 Can anyone explain this please.
Thanks, vijaya [ June 22, 2004: Message edited by: vijaya dev ]
when you perform a shift operation the value is promoted to at least an int so you get 11111111 11111111 11111111 11110011 after the shift you have: 01111111 11111111 11111111 11111001 if you chop off the higer 24 bits you remain with 11111001 which is -7!
(use two's complement to get the value in base 10: 11111001 1 - flip bits -->00000110 2 - add one -->00000111, which is 7 but you have to add the minus since it's negative)
[ June 22, 2004: Message edited by: Lukas Alamar ] [ June 22, 2004: Message edited by: Lukas Alamar ]