# How to implement unsigned subtraction using 2's complement?

I want to implement unsigned subtraction using 2's complement. I arrived to this, where if I have 2 4-bit binary numbers A and B, I transform B to 2's complement (let us name it B' so we don't get confused) and then add A to B'and I get the result C. This was the easy part for me.

Then if the final carry out is "0" I should transform C to 2's complement and add a "-" sign, but if the carry out is "1" I just don't adjust anything. And this is where it got me. I don't know how to implement it and I should put a led in the final result to let know if the sign is "+/-".

I think I should put a 2-to-1 multiplexer where the select input will be the carry out of the circuit I posted.

And by the way, my implementation is correct, whenever I put random values and then calculate those by hand I get the same result.

Thank you in advance.
(The program I used is called "logicCircuit".

So this is what I got to.(After editing my post) The circuit above is my main circuit.  These 2 pictures above are just zoomed in photos of the circuit

Now my question is, can I do something so I can get only one result. For example if the carry is "0" The multiplexer gives me the correct result, but in the same time I am getting a result in the upper part of my circuit which is the part that should give me a result whenever the carry is "1" and vice versa.

The gray bubble are leds so when the result is 1, the led lights up.

• RE " if I have 2 4-bit binary numbers A and B, I transform B to 2's complement ", how many bits is the 2's complement result? If it's 4 bits, what do you do if B > 7? – The Photon May 2 '20 at 14:53
• @ThePhoton the 2's complement result is also 4 bits. I don't want any of the inputs to be higher than 4-bits. – P_M May 2 '20 at 14:59
• @PeterMurr Just in case you aren't thinking about it, make sure that if you complement an input, that you also either complement the carry-in or set it to 1 depending on the operation you desire. – jonk May 2 '20 at 19:02

## 1 Answer

I think I should put a 2-to-1 multiplexer where the select input will be the carry out of the circuit I posted.

Yes, that's all there is to it.

You have a multiplexer. One input to the multiplexer is the output of the addition (A+B'). The other input of the multiplexer is the 2's complement of that (A+B')'. (Where we're using ' to indicate the 2's complement operation rather than simple inversion)

That means you're calculating the 2's complement of the addition every time, even though you only need it sometimes. That kind of "wasted effort" is very common in digital logic.

• But if I have 4 bits how do I link them to the 2-to-1 MUX? – P_M May 2 '20 at 15:05
• You get a 4-bit 2-to-1 mux. Or you use four one-bit 2-to-1 muxes. – The Photon May 2 '20 at 15:34