Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift Operator Question 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 Operator Question" Watch "Shift Operator Question" New topic

Shift Operator Question

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Hello everybody,
I am brand new in this forum and Java programming, hope to get helps from all of you.
I was stuck by the following question in Mock exam.
class J {
public static void main (String[] s) {
byte b = 0;
b += ~b >>> 1;
THe answer is -1. But how could that be? And I couldn't understand the explaination about this answer:
The entire four bits are shifted to the right and a zero replaces the most significant bit of the most significant byte; however, only the value of the least significant byte is printed.
More detailed explaination and examples are very appreciated.

SCJP 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The binary representation of ~0 is as follows.
Before the shift to the right, the left hand operand is converted to a 32 bit int. It is then shifted to the right one bit and zero is shifted into the most significant bit. The result is as follows.
01111111 11111111 11111111 11111111
If the above were printed as an int, the value would be Integer.MAX_VALUE. However, the compound assignment operator includes an implicit cast to the type of the left hand operand, so the result is cast to a byte by keeping the least significant byte and by throwing away the three most significant bytes. The result is as follows.
It is the two's compliment representation of -1.
The following are some additional examples of two's compliment representation of small integers.
I hope the above helps.
The Java Ranch Campfire Story about bits may be helpful.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Thanks for clarifying this, and thanks for providing the mock exams, which are very useful for the preparation.
May Pat
Ranch Hand

Joined: Jul 01, 2002
Posts: 32
I have a question on the sample question.
In your question:

This code compiled fine and returns a result.
However, if I do:

I got a compiler error saying possible loss of precision. Why the first one doesn't? I understand that when doing shift operation, bitwise operation or add operation, byte will be promoted to int then the operation is performed. The result is returned as int. If we want to assign it to a smaller primitive type, we need to explicitly cast the result. Does the reason that the original code work because += operation does some implicite casting? Thank you.

May P.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by May Pat:
Does the reason that the original code work because += operation does some implicite casting?

Yes. All compound assignment operators perform an implicit cast. Check out the JLS, §15.26.2 Compound Assignment Operators for more juicy details.

SCJP Tipline, etc.
I agree. Here's the link:
subject: Shift Operator Question
It's not a secret anymore!