programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Roundup #305

Rob Whelan
Ranch Hand
Posts: 33
I could figure out which answer was expected, but this question should probably be reworded or removed:
---------------------
(#305): What does the unsigned right-shift operator do to a negative number?

• keep it negative
• change its sign

• ---------------------
Well, the behavior most of the time is to change the sign, but try -99 >>> 32 (or -99 >>> 0 -- the same thing, really) -- for the less common result.
I understand that the question would get kind of awkward if you throw in "unless the number of bits shifted modulo the number of bits in the left-hand operand is 0" ... maybe you can just test the same knowledge by asking "would the result from -309482 >>> 23 be positive or negative?".

Cheryl Gray
Ranch Hand
Posts: 44
It seems as though Questions #304 & #305 contradict each other. This is what I understand:
eg. -176 >>2 = Neg #(according to #304) then pos # (according to # 305).

bill bozeman
Ranch Hand
Posts: 1070
Cheryl, I don't have the exam questions in front of me from the Rules Roundup, but from the way it looks, #304 is the signed right shift operator ( ">>" ) and #305 is the unsigned right shift operator ( ">>>" ). So they are not contradictory. -176 >>2 does give you a negative number as the signed right shift will look at the highest bit and add that in the shift, but unsigned will always add 0's so it will usually be positve.
As Rob pointed out, if the modulo of the number = 0 then nothing happens so it can still be negative. This can also be the case when you use the right shift on bytes, and then cast it back to a byte.
Rob, I beleive someone is working on the Rules Round-up game and adding more questions. I'll check, because you are right, the number can sometimes remain negative.
Bill

Paul Ralph
Ranch Hand
Posts: 314
George Brown is finishing up some revisions and I should get the beta from him to test in a few days or weeks.
I'll get a list of all the suggested improvements so I see if they have been implemented.
Paul R
------------------
I wanna be a sheriff when I grow up.

Cheryl Gray
Ranch Hand
Posts: 44
Thanks Bill. I also have a link which provides extra examples:Right & Left Shift Operators

 It is sorta covered in the JavaRanch Style Guide.