Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift Operator

 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q23.
What is the result of the following fragment of code?
(byte)0x81 >> 2
a)0x20
b)0x3FFFFFE0
c)0xE0
d)0xFFFFFFE0
The answer mentioned is (d).
(byte)0x81 >> 2 is equivalent to -127 >> 2 which results in -32.Should'nt it be -31 ? Furthur from this result how is (d) calculated ???
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Savio Mascarenhas:
Q23.
What is the result of the following fragment of code?
(byte)0x81 >> 2
a)0x20
b)0x3FFFFFE0
c)0xE0
d)0xFFFFFFE0
The answer mentioned is (d).
(byte)0x81 >> 2 is equivalent to -127 >> 2 which results in -32.Should'nt it be -31 ? Furthur from this result how is (d) calculated ???

Let me think a while..
0x81==>00000000 00000000 00000000 10000001
(byte)0x81==>100000001
msb is 1 so it will treat it as a negative no..
before appling right shift it will get converted in int..
so the msb is copied in all the first bits
==>11111111 11111111 11111111 10000001
(byte)0x81>>2 will result in..
1111 1111 1111 1111 1111 1111 1110 0000
F F F F F F E 0
So the result is fffffff0
hope u got it..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic