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

What's two's complement?

David Evans
Greenhorn

Joined: Dec 20, 2002
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

Joined: Dec 06, 2001
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
Then add 1:
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


Java API Documentation
The Java Tutorial
David Evans
Greenhorn

Joined: Dec 20, 2002
Posts: 3
Thanks !!
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
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.
Please check and comment:

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 ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Good job!
This is perfect correct. Have you tried negative numbers?
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
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

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jun 27, 2002
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's two's complement?