Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shift

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code will print
1: int i = 1;
2: i <<= 31; 1*2^31 = 2^31<br /> 3: i >>= 31; 2^31 /2^31 = 1
4: i >>= 1; 1>>=1 1/2 === 0
5:
6: int j = 1;
7: j <<= 31; 1*2^31<br /> 8: j >>= 31; 2^31/2^31 ==1
9:
10: System.out.println("i = " +i );
11: System.out.println("j = " +j);
A) i = 1
j = 1
B) i = -1
j = 1

C) i = 1
j = -1//answer how come???

D) i = -1
j = -1
 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where am i going wrong
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a problem like this, it's useful to print out intermediate results so that you can see for yourself if you're on the right track:


[This message has been edited by Jim Yingst (edited February 22, 2000).]
 
Jane Rozen
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who search for "shift" on this forum - make sure you follow Jim's advice - check the result!
Hint:it will be D) i=-1, j=-1 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic