This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question48 of Valentin Crettaz's mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question48 of Valentin Crettaz Watch "Question48 of Valentin Crettaz New topic

Question48 of Valentin Crettaz's mock exam

Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57

Exam explanation: the expression should be ((((4*6)-(3/2))<<(2*5))>>>((1%2-4))^3.
My question is that since 1%2-4=-3, can shift operator take negative integer as right operand? I can't find any comment/example like the code in many books.

I tried the following 2 code:

I got output 0 0 0.

I got output 1 0 0.

Could somebody tell me why?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You have j=j>>>n. Do you really mean that?
Don't worry about the sign of the righthand operand.
Just take the low-order 5 (int lefthand operand) or 6 (long lefthand operand) bits of the righthand operand and shift by that number of bits (taken as a positive number).
For example:
-1 >>> -1
The lefthand operator is an int so we take the
low-order 5 bits of the -1 on the righthand side. We get 11111, that's 31 as an unsigned number. Shift the lefthand side to the right by 31 bits, filling on left with 0s, gives
just 1 bit remaining in the lowest order position.
So -1 >>> -1 is 1.
All other cases are similar.
[ September 23, 2002: Message edited by: Barry Gaunt ]
[ September 23, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
The same question was posted before. I kept the answer to it as a reference. It might help:

Using % to reduce the right-hand side (RHS) operand is fine for positive operands. However, when you have negative RHS operands, you should go by the low-order bits (5 for int and 6 for long). That is,
x >> -4; // reduced to x >> 28 when x is int
x >> -4; // reduced to x >> 60 when x is long
What Joshua Bloch (author of Effective Java and the Collections API) pointed out to me a while back was that reduction is done by masking the RHS operand with 0x1F for int shifts and 0x3F for long shifts, not by using %.
For int:
-4 = 1111 11.. 1111 1100 (32 bits)
& 0x1F = & 0000 00.. 0001 1111
28 = 0000 00.. 0001 1100
For long:
-4 = 1111 11.. 1111 1100 (64 bits)
& 0x3F = & 0000 00.. 0011 1111
60 = 0000 00.. 0011 1100
shifting operands must be a primitive integer type, which of course includes long and excludes float and double.

And the answer to your original post is 3 by the way.
I hope this helps.

Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
I understand this question now, thanks greatly for your replies!
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: Question48 of Valentin Crettaz's mock exam
It's not a secret anymore!