This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

When I played with "rules round-up game", there's one question I can't understand. Sorry, I can't remember what exact the question is.

Q: what is the result if we do 270 >> 33? 1) 277 >> 1 2) 0 3) -1

I thought if you right shift more than the bits the number has, you always get 0 while the correct answer is "277 >> 1". I don't know how this come out and what's this mean.

[ April 12, 2005: Message edited by: l bb ] [ April 12, 2005: Message edited by: l bb ]

The >> operator only works on the number of bits in the primitive type. Because 270 is an int it only has 32 bits. The shift value is modded by 32 (shift%32).

If you did 270L it would be modded by 64, but in this case that would not change the shift operator.

lauren bai
Greenhorn

Joined: Apr 08, 2005
Posts: 18

posted

0

The >> operator only works on the number of bits in the primitive type. Because 270 is an int it only has 32 bits. The shift value is modded by 32 (shift%32).

Steven, thank you. I understand a little bit now. use int as example, does this mean if any number larger than 32 will be modded. i.e 64 will be equal to 0, so don't shift a bit. Why doesn't it give a compile error to say that you can't shift more bits than the primitive type has? Who does this mod for us, JVM?

[ April 12, 2005: Message edited by: lauren bai ] [ April 12, 2005: Message edited by: lauren bai ]

Originally posted by lauren bai: Steven, thank you. I understand a little bit now. use int as example, does this mean if any number larger than 32 will be modded. i.e 64 will be equal to 0, so don't shift a bit. Why doesn't it give a compile error to say that you can't shift more bits than the primitive type has? Who does this mod for us, JVM?

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

The JVM may not actually be performing a mod (%) operation as it normally would, but the end result is the same, assuming the right operand is a positive integer. The right-hand operand will effectively be modded by 32 before being used by the shift operator. So 64 acts like 0 and 32 acts like 0.

Yes, the JVM does the mod for us. The compiler doesn't give a compile-time error because the shift operator only uses the low-order bits of the right-hand operator, so there can be no problem with shifting more bits than the left-hand operator's bit depth.

Incidentally, the result is particularly difficult to calculate by hand when the right-hand operand is negative.

Hi Just I explain a simple formula to find the value

int has 32 bit so x=8 store like 0000 0000 0000 0000 0000 0000 0000 1000 if x>>33 then 33 is greater than no of bits for int. for this situation you find the remainder ie., 33%32=1 so bits shifted 1 bit right. so Ans is 4.

Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>

Originally posted by l bb: When I played with "rules round-up game", there's one question I can't understand. Sorry, I can't remember what exact the question is.

Q: what is the result if we do 270 >> 33? 1) 277 >> 1 2) 0 3) -1

I thought if you right shift more than the bits the number has, you always get 0 while the correct answer is "277 >> 1". I don't know how this come out and what's this mean.

[ April 12, 2005: Message edited by: l bb ]

[ April 12, 2005: Message edited by: l bb ]

Is that supposed to be 270 >> 1?

Be Afraid...Be very Afraid...

lauren bai
Greenhorn

Joined: Apr 08, 2005
Posts: 18

posted

0

Thanks Raghu! You explaination make me understood finnally. I thought the 5 lowest-order bits of the right-hand operand and & 0x1f is the point. Yes,Paulo. It should be 270 >> 1?

Thanks for everyone who give me help. I like this place now.