public class UnsignedRightShift{
public static void main(
String[] args){
byte b = -13;
b >>>=1;
System.out.println(b);
}
}
Here "b>>>=1;" is the same as "b = (byte)(b>>>1);"
Anything smaller than an int is promoted to an int, so b promoted to int which is "fffffff3"; and it is "7ffffff9" after shift. Then it casts int to byte which is "f9" (-7).
Please see
15.19 Shift Operators:
The type of each of the operands of a shift operator must be a type that is convertible (�5.1.8) to a primitive integral type, or a compile-time error occurs. Binary numeric promotion (�5.6.2) is not performed on the operands; rather, unary numeric promotion (�) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.
[ June 01, 2006: Message edited by: wise owen ]