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 operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operators" Watch "Shift operators" New topic

Shift operators

Faqeer Khudaka
Ranch Hand

Joined: Jan 24, 2006
Posts: 32

I found this question in one of the mock exams:

Correct answer is A & B. My answer was B&C cause when 128 >> 1 is computed, the rule is that whatever the most significant bit is, that has to be "filled" from the left. In this case when 128(10000000) is shifted right by 1 bit, another 1 will be added cause initially the most significant bit of 128 was 1. This will give us 11000000 which comes out to be 192.
Am i correct??

Jan Valenta
Ranch Hand

Joined: Nov 30, 2005
Posts: 32
Don't forget that there is automatic promotion of the operands to ints. Therefore you are shifting (00000000 00000000 00000000 10000000) and the initial zero stays zero.


Anybody knows about a job for <a href="" target="_blank" rel="nofollow">SCJP5.0, Ph.D. in Software Engineering, 2 years Java, C, C++</a> in Montreal, QC, Canada? Thanks!
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
i think the correct answer is 64 for both

considering 128 as int datatype

128 value is 00000000 00000000 00000000 10000000
based on the referred link above

>> 00000000 00000000 00000000 01000000 (fills with highest (sign) bit on the left side )
>>> 00000000 00000000 00000000 01000000 (fills with 0 bits on the left side )

Parallax - Change in observational position that provides a new line of sight.
Faqeer Khudaka
Ranch Hand

Joined: Jan 24, 2006
Posts: 32
Thanks guys.
I didnt pay attention to the int promotion thing.
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
remember this topic is on the 1.4 exam, but it's NOT on the 1.5 exam.

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link:
subject: Shift operators
It's not a secret anymore!