2
\$\begingroup\$

I need some really basic help here. Can I use a 4bit adder chip as a subtracter by using the 2's complement for the number to be subtracted?

\$\endgroup\$
  • \$\begingroup\$ You can do what you seem to want, but IMO it would still be addition. 2 + 1 is addition, and so is 2 + -1. \$\endgroup\$ – Wouter van Ooijen Jan 29 '14 at 7:23
  • \$\begingroup\$ Okay. Suppose I want to do a 7-5, instead I am obliged to use a 4Bit adder circuit and do a 7+-5. For the first four bits of seven, I'd input 0111 since it is the binary equivalent for 7. What would I input for 5? Please explain \$\endgroup\$ – ubuntu_noob Jan 29 '14 at 7:30
  • \$\begingroup\$ That is outside the scope of your question, but it's simple: you need the two's complement of 5, which is formed by inverting all the bits and then adding 1. \$\endgroup\$ – Wouter van Ooijen Jan 29 '14 at 7:59
  • \$\begingroup\$ that is how subtraction is implemented in logic. you invert the second term and invert the carry in and feed it to an adder. (invert and add one). \$\endgroup\$ – old_timer Jun 4 '17 at 12:05
13
\$\begingroup\$

In 2's complement, negation can be achieved by inverting a number and adding one (ie -A = ~A + 1). To subtract a number B from A, invert B, add 1 to it, then proceed to add that sum to A.

A - B = A + ~B + 1

In order to transform a normal adder IC into a subtractor, you need to invert the second operand (B) and add 1 (by setting Cin = 1 ). An Adder subtractor can be achieved by using the following circuitry.

enter image description here

Note that when the control signal SUB is low,

A = A
B = B
Cin = 0

Therefore, the computed sum will be A + B + SUB = A + B.

But if SUB = 1

A = A
B = ~B
Cin = 1

Meaning the computed sum will now be A + ~B + SUB = A + ~B + 1 = A - B, hence achieving subtraction.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you explain why the XOR is needed on the final Cout output? It makes sense that it would be there, to parallel the one at the initial Cin input, but I can't figure out why. \$\endgroup\$ – Jonathon Reinhart Nov 12 '17 at 16:27
  • \$\begingroup\$ @JonathonReinhart If SUB = 0 then the XOR is a basically just a buffer and just does nothing. If SUB = 1 (when subtracting) the value of Cout needs to be inverted to make sure both sign extension and overflow are handled correctly (like in the case when A = B = 0). \$\endgroup\$ – KillaKem Nov 12 '17 at 17:12
  • \$\begingroup\$ Right, I guess I'm trying to wrap my head around why Cout needs inverted when you're subtracting. Is it sufficient to just say "because B was inverted on the way in"? \$\endgroup\$ – Jonathon Reinhart Nov 12 '17 at 17:36
0
\$\begingroup\$

Of course you can do it, at a cost of a bit for sign

For a 4 bit chip, you can store unsigned number of 0 to +15 and signed number of -8 to +7 by 2's complement.

For subtraction, you of course need to do signed calculation with the first bit indicating the sign and rest of the bits, the value.

So, 5-2 is actually 5 + (-2) with binary representation of

  • 5 => 0101
  • -2 => 1110 (First bit being 1 for negative and the rest is the bit flip of 2 plus one by 2's complement. So 2 = 010, -2 = 101+1 = 110)

Adding the above in binary yields 0011 which translates to +3

Another example is 2-3

  • 2 => 0010
  • -3 => 1101

2 + (-3) = 0010 + 1101 = 1111

To translate 1111 back to base10 you'd subtract 1 and then bit flip the rest so,

1111 => 1110 => 0001

Equals to -1

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.