File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes >> shift question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ">> shift question" Watch ">> shift question" New topic

>> shift question

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Question: What is the result of the following fragment of code?
(byte)0x81 >> 2
A. 0x20
C. 0xE0
Answer is D. I understand the complier will widen the byte to an int before performing the shift, but could not figure out the result. Please help me. Many thanks.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
(byte)0x81 >> 2
0x81 = 1000 0001
It is first converted to an int before the shift.
11111111 11111111 11111111 10000001
After the shift it is as follows.
11111111 11111111 11111111 1110 0000 = 0xFFFFFFE0

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Thank you very much. I was stuck in the step to modify
1000 0001 -->11111111 11111111 11111111 10000001. I thought "0" was added before 1000 0001. Anyway, I understand it now. I appreciate your help!
I agree. Here's the link:
subject: >> shift question
It's not a secret anymore!