wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shift operator 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 operator" Watch "shift operator" New topic
Author

shift operator

nachagoni rishi
Greenhorn

Joined: Oct 14, 2004
Posts: 26
The following code will print

1: int i = 1;
2: i <<= 31;
3: i >>= 31;
4: i >>= 1;
5:
6: int j = 1;
7: j >>= 31;
8: j >>= 31;
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

D) i = -1
j = 0

the answer is d. can u please give me an explanation for the answer.
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi,
when you push 1 to 31 times left it overwrite the sign bit thus turning it to a neg number(1 << 31)
It will looks like 10000000...(put 31 zeroes) etc. To find out the real negative number take the two's complement which will give -2147483648. Now it is shifting to the right using the
>> signed shift operator, 111111111.... Taking the 2's complement we will get it is -1. Now if it is shifted
right it will continues to 1111111... making it -1. Thus the value of i is -1

In the second question the bit position is 00000000....1(put 31 zeroes), a single right shift itself push the 1 out and
it becomes 0. Thus further right shift makes no change. so j becomes 0.

For more info :
javaranch
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
hi rishi,

there is no complicacy.
U could guess the answer from the second option also.
In the second option,

j=1;
whose binary is
0000 0000 0000 0000 0000 0000 0000 0001

if we shift it by 31 bits, the out put is
0000 0000 0000 0000 0000 0000 0000 0000

So j =0;

and u can see that out of four options only one contains j=0;

U have to be tricky while answering any question

Arnab
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Hi there
even I was struggling sometime with these bit juggling game till some kind souls explained me how it's done :-
int i = 1;
2: i <<= 31;
3: i >>= 31;
4: i >>= 1;

line 2 results :-
10000000000000000000000000000000

line 3 results:-
11111111111111111111111111111111 [all gaps preceeding the '1' on line 2 results are prefixed with a '1']

line 4 results in:
11111111111111111111111111111111 [ drop the last '1' on line 3 result, and fill the empty space created LHS with a '1']

this # is -ve , see the '1' on the extreme LHS , which decides this fact.
so flip all bytes:-
00000000000000000000000000000000
and add 1
resulting in
00000000000000000000000000000001
thus the final outcome is -ve 1 or -1.

also go for Arnab's theory of elimination, in this situation that seems to be the most smartest thing to do...

apart from giving the Exam and cracking it maybe in style,what 'real life value' this bit game has, confounds reasoning.
nachagoni rishi
Greenhorn

Joined: Oct 14, 2004
Posts: 26
Thankyou vipin,Arnab NettyPoestel for the detailed explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shift operator
 
Similar Threads
The shift >> doubt .......................
Shift operator....
Shift operator doubt
shift
Shift operator