Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question48 of Valentin Crettaz's mock exam

 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code:

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?
Thanks.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
-Barry
[ September 23, 2002: Message edited by: Barry Gaunt ]
[ September 23, 2002: Message edited by: Barry Gaunt ]
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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.
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand this question now, thanks greatly for your replies!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chia,
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic