# What's two's complement?

David Evans

Greenhorn

Posts: 3

Layne Lund

Ranch Hand

Posts: 3061

posted 13 years ago

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

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

Garrett Smith

Ranch Hand

Posts: 401

posted 13 years ago

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 ]

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

Posts: 2545

David Weitzman

Ranch Hand

Posts: 1365

posted 13 years ago

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.

posted 13 years ago

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

http://www.coderanch.com/t/389872/java/java/remedial-help-binary-numbers

HTH,

- Manish

Garrett Smith

Ranch Hand

Posts: 401

posted 13 years ago

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 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 ]

comp.lang.javascript FAQ: http://jibbering.com/faq/