File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift Operator Question" Watch "Shift Operator Question" New topic
Author

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.
Code:
----------------------------------------------
class J {
public static void main (String[] s) {
byte b = 0;
b += ~b >>> 1;
System.out.println(b);
}
}
-----------------------------------------------
THe answer is -1. But how could that be? And I couldn't understand the explaination about this answer:
<I>
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.
</I>
More detailed explaination and examples are very appreciated.


SCJP 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Yan,
The binary representation of ~0 is as follows.
11111111
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.
11111111
It is the two's compliment representation of -1.
The following are some additional examples of two's compliment representation of small integers.
1=00000001
0=00000000
-1=11111111
-2=11111110
-3=11111101
-4=11111100
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="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Dan,
Thanks for clarifying this, and thanks for providing the mock exams, which are very useful for the preparation.
-Yan
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.
Corey


SCJP Tipline, etc.
 
jQuery in Action, 2nd edition
 
subject: Shift Operator Question