• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

S.o.p (byte) 0X81 >> 2

 
Rosie Nelson
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the result of the following fragment of code?
(byte) 0X81 >> 2

I am confused about the output of the following code. I undersatand that (byte)0X81 is promoted to an int - unary promotion. I would expect the following output:-

0000 0000 0000 0000 0000 0000 1000 0001 >> 2 gives rise to:-
0000 0000 0000 0000 0000 0000 0010 0000 = 0x20
but this is incorrect. Can somebody please explain where I'm going wrong?
 
Jennifer Warren
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try doing this (byte)(0x81 >>2);
check out the result.
Jennifer.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0000 0000 0000 0000 0000 0000 1000 0001

cast to a byte gives
1000 0001

Notice the 1 in the left-most position. Therefore it is a negative number (actually decimal -127).

Expand to an int
1111 1111 1111 1111 1111 1111 1000 0001

Shift right 2
1111 1111 1111 1111 1111 1111 1110 0000

It is still a negative number (decimal -32)
 
Rosie Nelson
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Marilyn.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic