Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2008-01-16 17:07:25

mikau
Member
Registered: 2005-08-22
Posts: 1,504

### two's complement number system

i'm taking a course in assembly language x_x

I'm reading about how a given computer system stores negative numbers, and they are not being very clear. After a great amount of confusion i've hypothesized that they use the regular binary system to store positive values, and two's complement numbers for negative values.

Supposedly, you can take the negative of a number by using NOT and adding 1

take 3

0011

NOT

1100

1101 this represents -3?

but that gives us 13, which if i'm not mistaken is the complement of 3, not -3.

is this correct? we store negative values by storing their absolute value in two's complement form?

Last edited by mikau (2008-01-16 17:09:38)

A logarithm is just a misspelled algorithm.

Offline

## #2 2008-01-16 17:11:39

mikau
Member
Registered: 2005-08-22
Posts: 1,504

### Re: two's complement number system

oops! I'm sorry. I thought i was posting this in coders corner! my bad!

A logarithm is just a misspelled algorithm.

Offline

## #3 2008-01-16 17:44:18

pi man
Member
Registered: 2006-07-06
Posts: 251

### Re: two's complement number system

The first bit (leftmost) is the sign bit and is not used in computing the value of the number.   Zero signifies a positive number, a one signifies a negative.   So with 4 bits, you can represent from 7 (0111) to -8 (1000).

Offline

## #4 2008-01-16 19:54:08

treehugger96
Member
Registered: 2007-12-24
Posts: 10

### Re: two's complement number system

i'm not sure but i think -3(101)

........"To my mind having care and concern for others is the highest of human qualities"
.........Fred Hollows

Offline

## #5 2008-01-17 01:23:34

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

### Re: two's complement number system

After a great amount of confusion i've hypothesized that they use the regular binary system to store positive values, and two's complement numbers for negative values.

Two's complement is "regular binary" for positive values.  I'm not certain if that's what you were trying to say.

In two's complement and ignoring the first bit, the lowest number is always represented by 000....1, and the highest number is represented by 11111...1.  This is true for positive and negative numbers.

01111111...1 is the highest positive value.  0111 is 7.
11111111...1 is the highest negative value.  1111 is -1.

00000000...1 is the lowest positive value.  0001 is 1.
10000000...0 is the lowest negative value.  1000 is -8.

From there, you just interpolate.  For each bit you add on to (1/0)00000, simply add 1 to the number.  This means:

0001 = 1
0010 = 2
0011 = 3

While:

1000 = -8
1001 = -7
1010 = -6

And so on.  Now the real question is why do we use this system?

"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

## #6 2008-01-17 03:08:15

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

### Re: two's complement number system

we use this system because we can use the exact same set of logic gates to add two normal binary integers, as we can to add two two's comlement binary integers. without any overhead such as 'if first bit is 1, then do this instead because it is negative' it can simply be treat as any other positive integer, and due to overflow's ends up giving the correct answer

example: 4 bit signed integers:

6 = 0110. -6 = !6+1 = 1001+1 = 1010

now if we add them together we get

0110+1010 = (1)0000. since the 1 is lost in overflow we get 0. the correct result

another example:

5 = 0100. -7 = 1001

add them together you get 1101 which is -2. the correct result.

Last edited by luca-deltodesco (2008-01-17 03:11:29)

The Beginning Of All Things To End.
The End Of All Things To Come.

Offline

## #7 2008-01-17 03:10:40

pi man
Member
Registered: 2006-07-06
Posts: 251

### Re: two's complement number system

It simplifies adding a positive number to a negative number.

-4          1100
+5          0101
==         ====
1         10001

-5          1011
+4          0100
==        ====
-1          1111

If there are 5 digits (in these examples) in the answer, the leftmost digit is ignored.

Offline

## #8 2008-01-17 03:22:36

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

### Re: two's complement number system

also note that this doesn't just work for binary numbers. if we define the negative integer as k-x where k is the number 1 followed by 'n' 0's where n is how many bits the number is. i.e. -0101 = 10000-0101 = 1011.

then lets do it in decimal.

-0485 = 10000-0485 = 9515.
0785+9515 = (1)0300 = 300. the correct result for 785-485.

The Beginning Of All Things To End.
The End Of All Things To Come.

Offline

## #9 2008-01-17 06:14:22

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

### Re: two's complement number system

There is one more thing (two, perhaps).  The other way to do negative numbers is to just have a sign bit, and the rest be interpreted as a normal positive value.  Where else does 2's complement succeed when this method fails.

"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

## #10 2008-01-17 13:31:59

mikau
Member
Registered: 2005-08-22
Posts: 1,504

### Re: two's complement number system

Wow! look at the response this threads getting!

Ricky wrote:

After a great amount of confusion i've hypothesized that they use the regular binary system to store positive values, and two's complement numbers for negative values.

Two's complement is "regular binary" for positive values.  I'm not certain if that's what you were trying to say.

by 'regular binary' i meant place value, as in 1010 = 1*2^3 + 0*2^2 + 1*2^1 + 0*^0 = 12d

What i really think my book is doing is using the ordinary binary system for positive numbers (as in, 1010 = 12d) and using two's complement for negative numbers. I was actually able to prove that there is no overlap so there is no ambiguity between the two.

we are using an online text for this course, and its accesible from anywhere:
http://webster.cs.ucr.edu/AoA/DOS/pdf/0_AoAPDF.html

if you click on chapter 1 and go to the pages 13 of the pdf (not of the actual page numbers) you can find the part on signed numbers and on the next page they do positive to negative conversions.

The reason i think they're using 'place value binary' for positive numbers and 'two's complement' for negatives is because supposedly, you can calculate a numbers negative by using NOT and adding 1. They begin with 5 written as 0101 and then write -5 as 1011. 1011 = 11d which by my understanding, is how we write positive 5 in two's complement form.

also, that pdf never actually told me what two's complement meant and i'm currently under the impression that if x is the value you want to represent (who's binary form has n bits) then
x + complement = 2^n
and we write x in two's complement form by writing 'complement' in binary form.

Thats my understanding of the subject. Care to point out where i'm going wrong, if anywhere?

Last edited by mikau (2008-01-17 13:38:05)

A logarithm is just a misspelled algorithm.

Offline

## #11 2008-01-20 06:11:28

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

### Re: two's complement number system

you're right about the complement part, its what i said above when i went on two 10's complement with decimal.

its two's complement because you take the power of 2 that is 1 above the range of the bits. and then subtract the number from it for its two's complement, which due to overflows is the same as inverting every bit and adding 1

The Beginning Of All Things To End.
The End Of All Things To Come.

Offline