aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shift question 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 question" Watch "shift question" New topic
Author

shift question

bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
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);
Ans is i = -1 and j =0;
could any one tell me how i is -1 ?
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
modify your code as follows:

each output line shows the binary representation of i.
Note, the first line shows 1 as its binary representation because the leading zeros are omitted.
Hope this helped. But basically it has to do with the fact that int has 32-bit size, and the left most bit is the sign bit that also happens to be the padding bit for >>. Remember >>> uses zero as its padding bit regardless of the sign bit.


Let us be showered in the light of confusion!
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
I didnt know about toBinaryString() .Thanks chung for helping me .
 
 
subject: shift question