# Shift operators

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

Leandro Oliveira
Ranch Hand
Posts: 298
45>>-6
is the same as
45>> (32-6)
hope this helps!
correct me if wrong!

Dan Chisholm
Ranch Hand
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.

Bert Bates
author
Sheriff
Posts: 8900
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 ).

stephen Kang
Ranch Hand
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.
That's why sun ask about the question. right?
I think kathy & berts book describes really well on this subject for the scjp2 test. i am enjoying the book a lot.