Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# What's two's complement?

David Evans
Greenhorn
Posts: 3
Another total newbie qusetion - what is two's complement? I understand binary and significant bits having been a COBOL programmer in the 70's but seem to have missed this bit - please explain.
Also what does UML stand for in class diagrams?

Layne Lund
Ranch Hand
Posts: 3061
UML stands for Universal Modeling Language. It is a way to diagram relations between classes and objects.
Two's compliment is the most common way to represent signed numbers in binary. To find the two's compliment representation of a negative number, first write the binary value of the positive number. Add enough leading zeros to fill all the bits used to store the number. Then flip each bit. In other words, change all 0's to 1's and vice versa. Then add 1 to the number.
For example, if we want to represent -4 in four-bit two's compliment. We first write 4 is binary:
0100
Then flip the bits:
1011
1100
The most significant bit is always 1 for negative numbers and 0 for positive numbers. Two's compliment also has several benefits for doing math in hardware. I won't get into that here. For a more detailed discussion, check out this page.
HTH
Layne

David Evans
Greenhorn
Posts: 3
Thanks !!

Garrett Smith
Ranch Hand
Posts: 401
I've only taken a few math classes in my life, so I am pretty weak in this area, so I worked on this for a while.
I found this is true:
1. divide input by radix, 2, and save the remainder as a string and save the number of times that 2 went into input as input, so input is now the result of input / radix.
2. repeat step 2 until the number is <= the radix.
3. save the last number of successful divisions
4. read the numbers in order of last calculated division to first remainder.
Here is an example to convert inp to binary:
inp = 19
19 / 2 = 9 r 1
9 / 2 = 4 r 1
4 / 2 = 2 r 0
2 / 2 = 1 r 0
1 --------- 1
10011 should be 19 in binary. Let's see:
(1*(2 pow 0)) + (1 * (2 pow 1)) + (1 * (2 pow 4))
(1) + (3) + (16) = 19
I wrote a program and it seems to work. I didn't have any help from anyone.

Would like any comments regarding this. I'd like to continue working on this so I can improve my skill in this area.
Thanks,

Garrett
[ December 28, 2002: Message edited by: Garrett Smith ]
[ December 28, 2002: Message edited by: Garrett Smith ]

John Lee
Ranch Hand
Posts: 2545
Good job!
This is perfect correct. Have you tried negative numbers?

David Weitzman
Ranch Hand
Posts: 1365
Actually UML stands for Unified Modeling Language. In addition to class diagrams, there are about eleven or so other UML diagram types. For more information, go to the website of the people who write the UML specs, OMG.

Manish Hatwalne
Ranch Hand
Posts: 2591
Have a look at this and see if it is of some help
http://www.coderanch.com/t/389872/java/java/remedial-help-binary-numbers
HTH,
- Manish

Garrett Smith
Ranch Hand
Posts: 401
Okay, my prog was bugged.
I changed the value of the mask:

was:

changed to:

It doesn't work for negative numbers though
[ January 01, 2003: Message edited by: Garrett Smith ]