programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

unsigned right shift

Greenhorn
Posts: 21
True or False?
Unsigned Right Shift on a Negative Integer always returns a Positive Integer.
the answer is given as false?
i am confused as unsigned right shift on a number is always positive.
regards
Praveena

Bartender
Posts: 2205
Hi Praveena.
for example,
-1 >>> 1 //unsigned right shift by one place
this evaluates to 2147483647, which is a very big positive nubmer, indeed there is none larger for an int.
Why? Well, you have to understand that the bitshift operators are operating on the numbers in their natural binary form. In binary, an int equal to -1 is represented thusly:
11111111111111111111111111111111 (32 bits).
When you right shift unsigned, you shift in zeros into the high order bit. so -1 >>> 1 is 11111111111111111111111111111111
>>>1 which is
01111111111111111111111111111111
This number in decimal is 2147483647.
In fact, ANY time you do an unsigned right shift, you end up with a postive number, because the only way a number can be negative is if the high-order bit is ONE, and when you do an unsigned shift, you always shift in a ZERO.
hope this helps!
Rob

Ranch Hand
Posts: 5040

Unsigned Right Shift on a Negative Integer always returns a Positive Integer.
the answer is given as false?
i am confused as unsigned right shift on a number is always positive.

Good one.
-1 >>> 32 = -1
Hope this helps.
You can try the bit shifts in the Applet at the bottom of the Cat and Mouse Story
on the Campfire section of Javaranch.
- satya

Rob Ross
Bartender
Posts: 2205
Hmm, for purposes of the test are we supposed to count that as an actual shift?
-1 >>> 32 resolves to -1 >>> 0, so no bits are actually shifted; it's an identiy transformation.
Does this one example mean you cannot say "unsigned right shift always returns a positive value"???
Rob

Ranch Hand
Posts: 5040

-1 >>> 32 resolves to -1 >>> 0, so no bits are actually shifted; it's an identiy transformation.

Thats a runtime call.

Does this one example mean you cannot say "unsigned right shift always returns a positive value"???

Ofcourse.
I know I wouldn't say that. Thats why its soooo .
regds.
- satya

Rob Ross
Bartender
Posts: 2205
This question is really making me think.
This is like saying "unsigned right shift of a negative number is always positive unless you're not shifting the number"...this seems strange to me.
What is the "official" answer supposed to be for the SCJP2??

Ranch Hand
Posts: 5040

What is the "official" answer supposed to be for the SCJP2?

I don't work for Sun or anything, but I would go with FALSE on this stmt. So finally,
True or False?
Unsigned Right Shift on a Negative Integer always returns a Positive Integer.

Examples:
-1 >>> 32 = -1
-43 >>> 32 = -43
and so on......
regds.
- satya
ps: You are assuming -1 >>> 32 == -1 >>> 0.
But apply your own theory you explained and literally shift by 32 bits and evaluate what you get. -1 >>> 32 == -1 >>> 0 , IMO, is only a convenience.

Praveena khandavalli
Greenhorn
Posts: 21