• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
please explain..
regards
Praveena
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveena.
The answer is wrong.
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
please explain.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

-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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

My Answer: FALSE.
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi madhav and rob,
so the sign of the number being shifted by bits depends on the number by which it is shifted ...as you have mentioned unsigned right shift by '32' wont change the sign of the negative number ..
so the unsigned right shift on a negative number is not always positive
so the answer is false
got it
thanx
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!