Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

A silly question?

lauren bai
Greenhorn
Posts: 18
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 ]

SandeepChicago Saini
Greenhorn
Posts: 29
Please refer to Chapter 3 of K & B book.

It is explained in the Exam Watch"s...

lauren bai
Greenhorn
Posts: 18
Hi, SandeepChicago:
I just can't understand the explain.....
and can't understant why the result is not a number but a expression 277 >> 1
It is explained in the Exam Watch"s..

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
"l bb"-

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark

lauren bai
Greenhorn
Posts: 18
I've changed.

Is there some one can explain that problem for me?

Steven Bell
Ranch Hand
Posts: 1071
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
Posts: 18
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 ]

Joe Sondow
Ranch Hand
Posts: 195
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?

In the shift operators section of the JLS it says this:

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.

Raghu Shree
Ranch Hand
Posts: 143
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.

Paulo Aquino
Ranch Hand
Posts: 202
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?

lauren bai
Greenhorn
Posts: 18
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.

 Don't get me started about those stupid light bulbs.