File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A silly 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 "A silly question?" Watch "A silly question?" New topic
Author

A silly question?

lauren bai
Greenhorn

Joined: Apr 08, 2005
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 ]

SCJP (In Progress)
SandeepChicago Saini
Greenhorn

Joined: Mar 11, 2005
Posts: 29
Please refer to Chapter 3 of K & B book.

It is explained in the Exam Watch"s...
lauren bai
Greenhorn

Joined: Apr 08, 2005
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

"l bb"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
lauren bai
Greenhorn

Joined: Apr 08, 2005
Posts: 18
I've changed.

Is there some one can explain that problem for me?

Thank you in advance
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
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

Joined: Apr 08, 2005
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

Joined: Apr 10, 2005
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
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.


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>
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A silly question?