File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operators " Watch "Shift operators " New topic

Shift operators

Pradeep bhatt
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;
Thanks in advance

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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="" target="_blank" rel="nofollow">Try my mock exam.</a>
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
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.
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.

Stephen Kang
I agree. Here's the link:
subject: Shift operators
It's not a secret anymore!