wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Two's complement question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two Watch "Two New topic
Author

Two's complement question

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
A question on the R&H CD is as follows:
class X {
public static void main(String[] a) {
try {
short s=0x00FD;
byte b = (byte)s;
System.out.println("b = " + b);
}
catch (Exception e) {
System.out.println("TROUBLE!");
}// end catch block
} // end main
} // end class X

Possible answers are as follows:
A) It generates a compiler error at line 4.
B) It generates a compiler error at line 5.
C) If the cast on line 5 were omitted, then line 5 would not compile.
D) The code compiles and prints "b = 0".
E) The code compiles and prints "b = -2".
F) The code compiles and prints "b = -3".
G. The code compiles and prints "TROUBLE".

Correct selectionj is: C, F
Line 4 compiles because the compiler allows literal assignments to bytes, shorts, and chars without requiring an explicit cast. The caset on line 4 is required because a short is wider than a byte.
At runtime, no exception is generated; the original 16-bit value is truncated to an 8-bit value (FD = 1111 1101) by having the high-order half discarded. To determine the value of this two's complement number invert all bits: 0000 0010. Then increment: 0000 0011. This is binary 3, so the value of FD is -3.
My question is how does one know to use the two's complement number? What are the rules for determining the value printed out in this question?
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
Here's some fun facts before explaining what's happening:
1) If the most significant bit (leftmost) of a byte, short, int or long is 1, then the number is negative.
2) To negate an integer value complement the bits and add 1.
For example the byte value 0000 0001 is 1. To make it negative, complement the bits (1111 1110) and add 1 (1111 1111). Likewise given 1111 1111 you know it's negative so to find its absolute value complement (0000 0000) and add 1 (0000 0001) so you know 1111 1111 is -1. This method of representing negative numbers is known as twos complement.
So in this example you have short 0x00FD which is hex for 253. It looks like 0000 0000 1111 1101 as a bit string. Casting this to a byte gives you 1111 1101. You know this value is negative - so complement (0000 0010) and add 1 (0000 0011) which is 3. So the value of b is -3.
Help yourself to the bits - they're complementary

[This message has been edited by Tod Tryk (edited June 09, 2001).]
Laurent Leonard
Ranch Hand

Joined: May 15, 2001
Posts: 35
Just a trick about the two's complement :
if you want to check that two numbers are opposite,
FD in hex -> 1111 1101 in bin
03 in hex -> 0000 0011 in bin
The sum -> 1 0000 0000 in bin
When it is truncated -> 0000 0000 in bin -> 00 in hex
=> 1111 1101 and 0000 0011 are opposite numbers.
------------------
Laurent Leonard
Laurent.Leonard@belgique.com
[This message has been edited by Laurent Leonard (edited June 11, 2001).]


Laurent LEONARD
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Tod,
You wrote:
So in this example you have short 0x00FD which is hex for 253. It looks like 0000 0000 1111 1101 as a bit string. Casting this to a byte gives you 1111 1101. You know this value is negative - so complement (0000 0010) and add 1 (0000 0011) which is 3. So the value of b is -3.
When you complement 1111 1101 and have 0000 0011 as the result, why is this now considered negative with the zero in the sign bit?
Thanks,
Betty
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
If there is a 0 in the sign bit the number must be positive. If there is a 1 in the sign bit the number must be negative.
The way I determine the decimal value of a negative binary number is first negate it (using two's complement), then convert the resulting positive binary to decimal, then add the - back.
Here's an example with a byte:
1110 0110 <- this is negative
0001 1001 + 1 = 0001 1010 <- two's complement (is positive)
This is +26 in decimal so the original byte = -26
Perhaps there are faster ways to determine the value of a negative binary number but this is how I do it. Anyone else?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Betty,
Just in case you were wondering why negative numbers are represented in two's complement. To humans, it may seem like an unnecessarily complicated way of doing things. Why not just set the sign bit to 1 or 0 and leave the rest as they are?
The reason for using the two's complement is because it simplifies calculations. Addition and subtraction are basically the same operation. To subtract B from A, just negate B and add it to A, ignoring any carry bits.
Take the expression (26 - 26). In binary, it would be
<pre>
0001 1010 // 26
1110 0110 // -26 (two's complement)
---------
1 0000 0000 // 0 (ignore carry)
</pre>
If you just used the sign bit but left the other bits as is, you couldn't do this:
<pre>
0001 1010 // 26
1001 1010 // -26 (only sign bit set)
---------
1011 0100 // ?? wrong answer
</pre>

Junilu - [How to Ask Questions] [How to Answer Questions]
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
What is the code to display the binary values as the calculation proceeds?
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
What is the code to display the binary values as the calculation proceeds?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Betty, the Integer and Long wrapper classes have a method toBinaryString() that will display the binary representation of a number. They also have the methods toHexString() and toOctalString().
Bookmark the page below for future reference:
Java API documentation
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Thanks. I think I finally have this resolved in my mind. It took a lot of work, but I'm there.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Thanks. I think I finally have this resolved in my mind. It took a lot of work, but I'm there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two's complement question