?
Solved

java thread

Posted on 2008-11-20
7
Medium Priority
?
230 Views
Last Modified: 2013-11-23
I assume that you all know about the unsigned right shift operator in Java

You'd all know that the positive decimal integer 2 is represented in binary as 10.

Therefore 2 would be represented in binary as 00000000 00000000 00000000 00000010 in a 32 bit integral type.


If you refer any book on Java ,the operator >>> would be superficially defined as
It shifts the bits on the left, by number of position to the right as specified by the operand on the right side, not preserving the sign.

So ,
2 >>> 1 would obviously result in 1 .
2 >>> 2 returns 0 (because it's shifted twice, and only there's 0 that remains)

Therefore for any other value on the right side greater than 2, it would result in 0.
2 >>> 3 returns 0
2 >>> 4 returns 0
2 >>> 67 returns 0
2 >>> 101 returns 0
2 >>> 1045 returns 0

So, you think the piece of code shown below is correct?

Code snippet 1:

if ( 2 >>> someIntegerValue == 0 ) {
System.out.println("Hey dude! I can assure you that the operand on the right side is definitely greather than or equal to 2");
} else {
System.out.println("The operand on the right side is definitely less than 2");
}


But when you run the program, for some values of someIntegerValue (for some valus greater than 2), it does not return 0.
Well, I'll tell you. 32, 33, 64, 65 ...

Why?

 If for upto 31 it returns 0, why for 32 would it return 2? What happens exactly?

Give me a proper explanation as to why does this happen.
(Don't tell me that the size of integer is 32 in Java , and every multiple of 32 is considered as 0. Well, that's right actually. But that's not the explanation I'm looking for).



Code snippet 1:
 
if ( 2 >>> someIntegerValue == 0 ) {
System.out.println("Hey dude! I can assure you that the operand on the right side is definitely greather than or equal to 2");
} else {
System.out.println("The operand on the right side is definitely less than 2");
}

Open in new window

0
Comment
Question by:akiles99
7 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 1000 total points
ID: 23002729
An int is 32 bits.

I assume that the code "sees" no use in shifting over the width of the var.

But I agree that 0 had beeen a better result.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 23002756
I overlooked the last of your post.

Anyway, the JDK does not contain the source(s) of the runtime arithmetic.
You will have to report this as a bug to SUN.

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23003182
The second line will be printed for values

0,1,
32,33
64,65
96,97
...


(exactly as expected)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 23003193
(At least that's what *i* expect ;-) If you don't, tell us why not
0
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 1000 total points
ID: 23006944
This behavior is per the JVM spec:
(for ishr instruction)  "..as if [shift value] were subjected to a bitwise logical AND with the mask value 0x1f."

Why Java implementors came to this decision, I do not know.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23007664
>> "..as if [shift value] were subjected to a bitwise logical AND with the mask value 0x1f."

IOW - it wraps ;-) Don't most (all?) ops?
0
 

Author Closing Comment

by:akiles99
ID: 31518639
THANKS
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question