File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift Operator" Watch "Shift Operator" New topic
Author

Shift Operator

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
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

Joined: Dec 01, 2000
Posts: 118
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..

<B>Bhupendra Mahajan</B>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift Operator