# 1's and 2's complements of binary numbers

Discussion in 'Math' started by PG1995, Oct 10, 2011.

Not open for further replies.

Joined:
Apr 15, 2011
Messages:
753
Hi

I request you to keep you replies as simple as possible and keep everything simple. Thanks.

Most computers (I'm not sure if all) do most of the arithmetic using addition operation - i.e. subtraction, multiplication, and division all use addition operation. This makes circuit design simple and with only one circuit you get to do different things.

What I guess 1's and 2's complements let us do all operations, subtraction, division, and multiplication using addition method.

complement (noun)
2 c : a number that when added to another number of the same sign yields zero if the significant digit farthest to the left is discarded —used especially in assembly language programming
[M-W's Col. Dic.]

In base 10 the compliment of 3 would be 7 because 3+7 = 10 and after discarding the 1, we are left with 0

Thus 4 is the compliment of 6, and 8 of 2, etc.

The compliment of 55 would be 45, because 55+45=100 and removing the 1 leaves you with 0 again.

With binary, the compliment of 1 is 1 because 1+1 =10

The compliment of 1010 would be 0110 (which is 2's complement of 1010) because 1010 + 0110 = 10000 in binary.

1: Let's say we have a binary number 110 (6). It's 1's complement would be: 001 (1), and 2's complement would be: 010 (2). I don't see what 1's and 2's complements of the number tells us. Please help me with it.

2: Perhaps, using a particular example could help us a bit. We have binary number 1010 (10) and we want to subtract 110 (6) from it - i.e. 1010 - 110. In base 10 arithmetic it would be 10 - 6 = 4. So, how do complements help us here?

1's complement of 110 = 001 (1)
2's complement of 110 = 010 (2)

I do have more related queries on this topic but I would ask them after clearing this up. Thanks.

Scanned pages which can be useful:
1: http://img510.imageshack.us/img510/2465/floyddigitalfundamental.jpg
2: http://img836.imageshack.us/img836/2465/floyddigitalfundamental.jpg

Last edited: Oct 10, 2011
2. ### GeoracerModeratorStaff Member

Joined:
Nov 25, 2009
Messages:
4,974
Location:
Athens, Greece (GMT +2)
Okay, first a very important clarification. All complement talk must be held under a specific word size. That is, we will assume all numbers have a standard bit number.

For our example, all numbers will be 4bits long.

The purpose of complement numbers is to allow us to perform subtraction through the addition of negative numbers.

In our decimal system, we know that the subtraction 5-2 can be re-written as 5+(-2). The number -2 is the opposite of 2, because 2+(-2)=0

In the binary system, we cannot use a "-" sign to communicate with the computer. Instead, we put a '0' in the Most Significant Bit of the word if the number is positive, or a "1" if the number is negative.

We will now explain how to obtain the 2's complement and then see how it works.
1. We invert all the bits of the number.
2. We add "1" to the number.

That said, the complement of 0011 is 1100+1=1101.

0011 has 0 in its MSB and therefore is positive. Its value in decimal is 3.
1101 has "1" in its MSB and that tells us that it is a negative number, produced by the 2's complement procedure. In order to find its value we must revert it to positive: 0010+1=0011. Therefore, that number is -3.

If we add them we get: 0011+1101=10000. However, since we work with only 4bits, the first 1 is overflowed and not taken into account. Thus the result is 0000.

We confirmed that by adding a number to its opposite the result is 0, and the notion of the complement works.

Let's see some more complements in action:
7-5=2 (-5 $\rightarrow$ -0101 $\rightarrow$ 1010+1 = 1011)
0111+1011=0010

3-6=-3 (-6 $\rightarrow$ -0110 $\rightarrow$ 1001+1 = 1010)
0011+1010=1101
The "1" in the MSB tells us that the result is negative and in order to find its absolute value, we must take its 2's complement: 1101 $\rightarrow$ 0010+1 = 0011.
Thus the result was -3.

I tried to tell you more about the use of the 2's complement rather than its theoretical place in the binary arithmetic. I hope things are clearer to you now.

Feel free to ask further if you have any questions.

Nyameko and PG1995 like this.

Joined:
Jul 7, 2009
Messages:
1,584
Location:
Northwest USA
You'll want to note that "complement" and "compliment" are two different words; for your topic, you want to use complement.

If you have python and want to play around with fixed-size integers using 2's-complement, you can download a script I wrote a couple of years ago here. Open up the zipfile and take a look at integer.py; read some of the comments on the various methods, especially __neg__, as they will describe some of the things you need to know about using 2's complement.

Joined:
Apr 15, 2011
Messages:
753
Thank you very much, GeoRacer.

I would ask some questions which might help me to understand this complement topic in a better way.

My book uses 8 bit number size.

The book I'm using says that 2's complement is most used these days. Further it says that a signed integer in binary can be represented in three forms: sign-magnitude, 1's complement, 2's complement. It also mentions that sign-magnitude is rarely used. From this, I conclude that most computers these days implement 2's complement to handle negative numbers.

I'm actually confusing sign-magnitude and 2's complement methods. In sign-magnitude if you have "1" at MSB of a number, then it tells us that the number is negative. But the same thing is said, in somewhat indirect way, that if you have "1" at MSB then it's negative, and if it's "0" it's a positive number.

Now I don't see how you can simply say by looking at "1" in MSB that it was produced by 2's complement. I can say it's regular 1101 (13). Why can't I? Okay, then tell me how would you represent normal 13 in binary?

In normal arithmetic we do the opposite if we want to go back to the original. 6x2=12. To get back to "6", we have to divide by "2". I would de-complement a number this way: number - 1 = result. Now invert bits of the result.

Let's talk about complementary method in base 10 which we might find more natural and easy. If we want to do "12 - 11 = 1", we find "11"'s complement which is "9" because 11+9 = 20. Now 12+9=21 and if we ignore the leftmost digit which is "2", then we are left with 1. Suppose we want to do 12-13=-1 by complementary method. Complement of 13 is 7 because 13+7=20. Now we add 7 to 12, 12+7=19. In this case after ignoring leftmost digit we are left with a wrong answer, "9". Where did I go wrong?

It is said that in 1's complement we have two values for "0". In 8 bit number size they would be: 0000 0000 and 1111 1111.

How can you say that "1111 1111" would equate to zero?

By the way, why are 1's complement and 2's complement called so? I mean what would it mean when it is said 3's complement? In base 10 we have only one complement method but base 2 has two different methods.

Thank you for your help. Please note that most the time I add extra details (which you people might find redundant) for my own later reference.

Regards
PG

Last edited: Oct 10, 2011
5. ### PapabravoAAC Fanatic!

Joined:
Feb 24, 2006
Messages:
7,700
Location:
Michigan, USA (GMT-5)
You can do arithmetic with either 1s complement or 2s complement notation. The major differences are:

1. In 1's complement notation you have an end-around carry, in 2s complement you do not.
2. In 1s complement notation there is a +0 and a -0, in 2s complement there is only one 0.

6. ### GeoracerModeratorStaff Member

Joined:
Nov 25, 2009
Messages:
4,974
Location:
Athens, Greece (GMT +2)
It's all about which standard and convention we choose to use.

It is correct that the 2's complement standard uses a sign bit, just as the sign-magnitude standard. But numbers that are formed with the latter standard can't be subtracted as they are, in contrast with the 2's complement numbers.
Both standards can be used to do the same operations, but with the sign-magnitude representation you need much more circuitry to do the transformations and checks of the results. It's all a matter of convenience.

Now, in the 2's complement standard, if you set the number size at 4 bits, you automatically reserve the MSB for the sign. If you want to write the number 13 you need to have at least 5 bit number size. You could read 1101 as 13, but you are no longer in the 2's complement format.

The complement in all mathematical spaces is a two-way notion. If the complement of A is B, then the complement of B is A. The way to go from A to B is the same as the one that gets you from B to A. Thus the way of finding the complement of 1101 is 0010+1=0011 and the complement of 0011 is 1100+1=1101.
Don't think of it as a mathematical equation. I doesn't work that way.

As far as 1's complement is concerned, 1111 1111 is zero, simply because the MSB is 1, which makes it negative and we have to invert it to see its value: 1111 1111 becomes 0000 0000 which is obviously 0.

I would advice you to stop making connections between the binary and the decimal system. My brain to the least gets more confused in his effort to find the similarities, where they are actually very few.

I could say to you to the least that in a base X format, you can find complements from 1 to X of any number, always given the digit size of the number. The complement in regards to N of a number Y is NNNN-Y
For example, in base 2, the 2's complement of 0100 is 10000-0100.
In base 2 again, the 1's complement of 0100 is 1111-0100.
In base 10, the 5's complement of 0023 is 5555-0023.
In base 10, the 10's complement of 0023 is 10000-0023.

Does that give you an idea of how it works?

PG1995 likes this.
7. ### MrChipsModeratorStaff Member

Joined:
Oct 2, 2009
Messages:
11,727
PG1995, you are complicating things unnecessarily.

There are millions (literally) of ways that numbers (even just integers) can be represented on a computer (even just 8 bits).

1's Complement is simply a bitwise NOT gate, i.e. you invert each bit. 1011 becomes 0100.

2's Complement is the most commonly used representation of signed integers because it obeys the rules of addition and subtraction. (BTW unsigned integers can by represented by another million number of ways but let us ignore that for now).

If you add 1 to 1111, you get 0000. Hence 1111 should be -1.

If you add 0010 and 1110 you get 0000. Hence one should be the negative value of the other. As it turns out, the MSB in these cases appears to distinguish positive from negative. So we accept it.

That's all there is to it. Try to accept it and don't go any further.
(If you still have questions feel free to ask.)

PG1995 likes this.

Joined:
Apr 15, 2011
Messages:
753

@someonesdad: Thank you for offering me that Python code. Sadly I don't know any programming though I'm trying to learn some of C++.

@MrChip: Perhaps, you are right that I complicate things unnecessarily. But we all learn in different ways. No one likes to make things complex for oneself. Personally, I wish I could learn the other way. Thanks.

Q: What is this "end-around carry"? I know that if the word size is 4 bit, then any carry for the 5th bit will be discarded. e.g. For 4 bit word size, "1" in "10000" will be discarded.

Q: In binary system 2's complement is the maximum complement one can find. I mean to say you can't think of finding 3's complement in base 2. As I have been told 2's complement doesn't give create any problem as compared to 1's complement which has two representation for 0. I'm just curious to know if this also happens in other bases such as base 10 that using, say, 4's complement gives problems such as two representation for 0.

Q: That means I can use 4 bit word size to represent only 7 positive numbers.

I'm not making any comparisons. I'm using base 10 which it makes more sense to me. If I can understand the complement in base 10, then I can translate the understand to base 2.

Q: In base 10, the 5's complement of 0023 is 5532 (5555-0023). What does it tell us? Suppose we want to do: 0029 - 0023.

The following text in blue is from my previous postings above:

Let's talk about complementary method in base 10 which we might find more natural and easy. If we want to do "12 - 11 = 1", we find "11"'s complement which is "9" because 11+9 = 20. Now 12+9=21 and if we ignore the leftmost digit which is "2", then we are left with 1. Suppose we want to do 12-13=-1 by complementary method. Complement of 13 is 7 because 13+7=20. Now we add 7 to 12, 12+7=19. In this case after ignoring leftmost digit we are left with a wrong answer, "9". Where did I go wrong?

/////////////

In base 10 the compliment of 3 would be 7 because 3+7 = 10 and after discarding the 1, we are left with 0

Thus 4 is the compliment of 6, and 8 of 2, etc.

The compliment of 55 would be 45, because 55+45=100 and removing the 1 leaves you with 0 again.

Today in a book I read that how to find the complement of a number:
Q: According to this method complement of 3 would be 6, not 7. And further 3+6 = 9, not 10. This even negates the M-W's definition I used in my first post.

9. ### GeoracerModeratorStaff Member

Joined:
Nov 25, 2009
Messages:
4,974
Location:
Athens, Greece (GMT +2)
Yup. But you also get another 7 negative plus a zero, so the resolution is still 15 numbers per 4 bits.

Lemme take a shot at it: 0029+5532=5561. Obviously there is an overflow and thus the result must be negative. We will complement the result to find the results absolute value: |5555-5561|=|-6|=6
Bingo! (Didn't think that would work so well)

Lastly, your book reference wasn't at fault. It's just that it only applies to the <base>'s complement of a number in the <base> numbering system. Check these two examples:
a)9-3
10's complement of 3 is 6 (by his method) which makes it
9+6=15
We disregard 1 as instructed by step 3 and the answer is 5.

b)5-7
10's complement of 7 is 2.
5+2=7
There is no carry thus the answer is
-complement(7)=-2

I 'd like to say here that this is pretty deep in number theory and you shouldn't worry understanding it if your goal is only to learn the binary system and digital circuitry.

PG1995 likes this.
10. ### MrChipsModeratorStaff Member

Joined:
Oct 2, 2009
Messages:
11,727
Here we go again.
If you want to use a single digit of base 10, the numerals are 0,1,2,3,4,5,6,7,8,9.
But you are mixing up unsigned with signed numbers. If you want signed numbers then the conversion is:
0 0
1 1
2 2
3 3
4 4
5 -5
6 -4
7 -3
8 -2
9 -1

Therefore the value 9 does not exist. 9 is really -1. So your result is -1, the correct answer.

PG1995 likes this.
11. ### PapabravoAAC Fanatic!

Joined:
Feb 24, 2006
Messages:
7,700
Location:
Michigan, USA (GMT-5)
The end-around-carry means that you take the carry bit and add it to the result of the addition. In 4-bit words
Code:
   0 1 1 1  -->  7
+  1 1 1 0  --> -1
-----------------------
1 0 1 0 1       6

So here we have a 4-bit result of 0101 == 5 and a carry
Code:
   0 1 0 1  --> Result of 5
+  0 0 0 1  --> Carry from the original addition
-------------
0 1 1 0  --> True result of 6

That was an end-around-carry

PG1995 likes this.

Joined:
Apr 15, 2011
Messages:
753
Thank you very much, GeoRacer, MrChips, Papabravo.

I'm sorry to say this but I'm still a bit confused. But the good thing is I can understand the topic in my book. What I'm inquiring about here is for my personal learning. But as advised by GeoRacer I won't pursue the topic further after finishing this discussion. Please be patient! Thanks.

In binary system 2's complement is the maximum complement one can find. I mean to say you can't think of finding 3's complement in base 2. As I have been told 2's complement doesn't give create any problem as compared to 1's complement which has two representation for 0. I'm just curious to know if this also happens in other bases such as base 10 that using, say, 4's complement gives problems such as two representation for 0.

For example, in 2's complement in the binary, MSB is a sign bit. We can know by looking at the binary if it's a negative one or a positive (assuming we know that we are using complementary method). Here, in the decimal, you have overflow condition to infer the negativity of the number. Is this so?

Do we discard carry in case of decimal too if there is one?

If I was using doing this: 9-3. I would do it as follows: 3's complement is 7 because 3+7=10 (discard the "1"), now 9+7=16. Obviously, my method doesn't work here. Where am I going wrong? Please help me.

So, we have two side by side sets. MrChips, I have a big confusion here. So, please don't mind my question. Suppose we want to use double digits of base 10, then I think we have numbers from 0 - 99.

Suppose I want to do (-4 x 2 = 8) using complementary method. What would be -4's complement? 96 or 6? I'm saying this because (4+96=100) and (4+6=10).

13. ### MrChipsModeratorStaff Member

Joined:
Oct 2, 2009
Messages:
11,727
If you want signed numbers, you will have to either divide your value range in half or double the number of states.
So if we want 0 to 99 in both positive and negative:

0 0
1 1
:
:
99 99
100 -100
101 -99
:
:
191 -9
192 -8
193 -7
194 -6
195 -5
196 -4
197 -3
198 -2
199 -1

So -4 x 2 is 196 x 2 = 392 = -8

14. ### GeoracerModeratorStaff Member

Joined:
Nov 25, 2009
Messages:
4,974
Location:
Athens, Greece (GMT +2)
@PG1995

You quoted it yourself:
That said, the complement of 3 is 10-1-3=6 and 9-3->9+6->15->1+5->6

What you did wrong is the calculation of the complement. I know that 3+6 isn't equal to 10, but your source's method works, so I 'll stick to it.
As I said, I don't master the art of enumeration methods and numerical analysis.

Joined:
Sep 24, 2014
Messages:
3

16. ### NyamekoNew Member

Joined:
Sep 24, 2014
Messages:
3

Hi MrChips 1
Can't you explain for me on the following problems:
1 A = 27.75 and B =1C,Bbase16. Calculate A-B in binary by make use of the 1's complement method. Give the final answer in octal system, All calculations and coversion must be shown.

2 A=256.56 base 8 and B=219.25 BASE 10. Convert A and B to binary numberes, then determine A-B by making use of the 2's complement method. Express the answer as hexadecimal number

17. ### MrChipsModeratorStaff Member

Joined:
Oct 2, 2009
Messages:
11,727
Do not hijack a 3-year old thread.