This week's book giveaway is in the Jobs Discussion forum.We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!

# Shift operators

Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

How do shift operators behave when the right hand operand is negative.
For e.g.
45 >> -6;

Groovy
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
45>>-6
is the same as
45>> (32-6)
hope this helps!
correct me if wrong!
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
When shifting a primitive int the shift distance is always between 0 and 31 inclusive. Just assume that the bit value of the shift distance is always "anded" with 0x1f. The binary value of -6 is
1111 1111 1111 1111 1111 1111 1111 1010
And the above with the 0x1f.
0000 0000 0000 0000 0000 0000 0001 1111
The result is 26.
0000 0000 0000 0000 0000 0000 0001 1010
I doubt that you will see a negative shift distance on the real exam, but Kathy or Bert would be a more authoritive source of information concerning what might actually appear.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8898

5
Howdy Bit Shifters -
'At this point', I don't think you'd see a negative shifting question on the exam, but the exam is not cast in stone. I think this is a valid discussion because it 'might' come up in an exam revision.
There are certain topics that seem way out of scope, based on knowing some of the exam maker's philosophies, but I could imagine this one sneaking in.
An example of the type of question that I don't think is likely to sneak in, is one where a detailed knowledge of precedence is required... the test makers all seem to be pretty strong advocates of parentheses.
CAUTION: All of this is speculation on my part - maybe safe bets, but definitely NOT anything I'd bet my life on. (Although I'll take \$5 bets ).

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
stephen Kang
Ranch Hand

Joined: Jul 26, 2002
Posts: 53
comment >>
I've felt bit shifting and bit cast representation is kind of important subject while one is dealing with network socket programming. CRC-check and encrption & decrption parts is all about bit shifiting.