[SOLVED] How to add 3 or more two's complement binary numbers

Status
Not open for further replies.

panda1234

Full Member level 2
Joined
Jan 22, 2015
Messages
125
Helped
4
Reputation
8
Reaction score
4
Trophy points
18
Visit site
Activity points
1,172
Hi,
We know if we want to add to number in two's complement we can do first sign extension and then add to number in this way if we have a carry in last we ignore carry.
But in 3 or 4 number we may have carry more than 1 in this way what do we do with carry?
(Please explain me with example)
thanks
 

Exactly the same, just take the total of numbers 1 and 2, add the total with the carry to number 3 then using that as the new total add it and the carry to number 4. Do it progressively rather than trying to handle several carry bits at once.

Brian.
 

Thanks but can you take an example? for example add 1111+1001+1111
 

here is an example of adding up some positive and negative numbers:
Code:
[FONT=Courier New]14
-9 = +5
+8 = +13
-12 = +1
-10 = -9

binary => 2's comp
 01110 => 01110
-01001 => 10111
 01000 => 01000
-01100 => 10100
-01010 => 10110

Adding them up
 01110
+10111
=00101 +5 (note there is a carry of 1 here, which is thrown away)
+01000
=01101 +13
+10100
=00001 +1
+10110
=10111 -9[/FONT]

- - - Updated - - -

Thanks but can you take an example? for example add 1111+1001+1111

That is -9 which can't be represented in 4-bits if we sign extend:
Code:
 11111
+11001
=11000
+11111
=10111
 

I had a feeling you were going to make some comment about doing it in one step. As I'm a hardware type, I was thinking (hoping) you weren't just asking about the math, but wanted to know how to implement the math in hardware.

Thanks but you adding them two by two i want to adding them all in one step.

You could do that but that makes for an unnecessarily complex design. If this is just a binary 2's complement math exercise then:

Code:
c4  1                  carry from 4th column
c3  1   0              carry from 3rd column
c2      1   0          carry from 2nd column
c1          1   0      carry from first column
    1   1   1   1   1  -1
  + 1   1   0   0   1  -7
  + 1   1   1   1   1  -1
  + 1   1   1   1   1  -1
  + 0   1   1   1   1  +15
  110 110 101 100 101  number of 1's in the column
  = 0   0   1   0   1
= +5

to ensure the calculation doesn't over/under flow you should probably sign extend by half the number of adds (e.g. in this case sign extend by 2 bits, 4 adds, before hand to ensure you'll never over/under flow)
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…