Could someone confirm whether my understanding is correct (step by step)?
From the mockup exam,
http://joppa.appliedreasoning.com/javaCert/html/exam.html Ques No: 23
(byte) 0x81 >> 2;
Steps:
1)
a) Each hex digit must be represented in four binary digits (zeros and ones) and each octal digit must be represented in three binary diigts. So, when we represent 8 in four binary digits, we get 1000. Similarly 1 is equivalent to 0001.
So, the binary representation of 0x81 is
0x 0000 0000 0000 0000 0000 0000 1000 0001
2) Since cast operator has higher precedene than shift operator,
(byte) 0x81 is now
0x 1000 0001 (truncated to byte)
3)
a) Now, when we apply shift operator, L.H.S should be promoted to int( if it is short, char or byte).
b) Since sign bit is 1, we need to extend the byte to int with bit 1. So,
promotion to int gives us
1111 1111 1111 1111 1111 1111 1000 0001
c) now shifting to two bits right, we get
1111 1111 1111 1111 1111 1111 1110 0000 (since the sign bit is 1, we use 1 bit to fill up the gap produced by shift)
So, the result is 0xFFFFFFE0
( I saw the answer is correct, but i want to know whether the logic what i am thinking is correct...)
**********************************************************
Next question from the same mock up exam (Ques no: 24)
0x81 >> 2
As step 1 (above): we get
0000 0000 0000 0000 0000 0000 1000 0001
now, shifting 2 bits right gives us
0000 0000 0000 0000 0000 0000 0010 0000 (here the sign bit is 0,so we use 0 bit to fill the gap)
So, the result is 0x20
Right?
******************************************************
Next question from the same mock up exam (Ques no: 25)
0x1 << 36
Here the L.H.S is int. So, we need to reduce the right hand operand to modulo 32, which gives us
0x1 << 4
Now, 0000 0000 0000 0000 0000 0000 0001 << 4 gives us
0000 0000 0000 0000 0000 0000 0001 0000 (for left shift, we need the gap with zeros)
So, the result is 0x10
If i understand wrongly, then please do correct me?