Here are the quotes from
JLS:
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8):
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (�4.2.1).
int i = 0x81;
The value of Ox81 in decimal system is 129.
The binary representation is 0000 0000 0000 0000 0000 0000 1000 0001
byte b = (byte) i;
When the value is casted to byte the last 8 bits are stored in the variable b.
The binary representation is 1000 0001 (the most significant bit is the sign bit).
Now value of b in decimal system is -127.
System.out.println("value of b >> 2 in Decimal: " + b >> 2);
Following are the quote from Mughal and Rasmussen book:
Since char, byte and short operands are promoted to either int or long, the result of applying these bitwise operators is always either an int or a long value.
Additional reading on
Shift Operators from JLS. Now the byte b variable is promoted to int.
The binary representation is 1111 1111 1111 1111 1111 1111 1000 0001
The decimal value is still -127.
The Hex representation is ffffff81
After the shit operation >> (shift right with sigh bit) the return value is int in this case.
The binary representation is 1111 1111 1111 1111 1111 1111 1110 0000
The decimal value is -32.
The Hex representation is ffffffe0
Hence the answer.
Another one related to that is question 25: give the result of 0x1 << 36<br /> The answer says it uses 36 mod 31 to get 0x1 << 5, I thought it was modulo 32? Anyway I am not sure how to test this, when I try and run something like this, I get an error saying "left shift exceeds size of the type being shifted"<br /> <br />
<br /> You are right on this one. You can verify by running the following code and reading the above mention JLS on Shift Operators.<br /> Note: Moderators this can be moved Mock Exam Errata.<br />
[This message has been edited by Manju Swamy (edited April 26, 2000).]