In digital circuits, an adder–subtractor is a circuit that is capable of adding or subtracting numbers (in particular, binary). Below is a circuit that does adding or subtracting depending on a control signal. It is also possible to construct a circuit that performs both addition and subtraction at the same time.

## Construction

A 4-bit ripple-carry adder–subtractor based on a 4-bit adder that performs two's complement on ${\displaystyle A}$ when ${\displaystyle D=1}$ to yield ${\displaystyle S=B-A}$

Having an n-bit adder for ${\displaystyle A}$ and ${\displaystyle B}$, then ${\displaystyle S=A+B}$. Then, assume the numbers are in two's complement. Then to perform ${\displaystyle B-A}$, two's complement theory says to invert each bit with a NOT gate then add one. This yields ${\displaystyle S=B+{\overline {A}}+1}$, which is easy to do with a slightly modified adder.

By preceding each ${\displaystyle A}$ input bit on the adder with a 2-to-1 multiplexer where:

• Input 0 (${\displaystyle I_{0}}$) is ${\displaystyle A}$
• Input 1 (${\displaystyle I_{1}}$) is ${\displaystyle {\overline {A}}}$

that has control input ${\displaystyle D}$ that is also connected to the initial carry, then the modified adder performs

• addition when${\displaystyle D=0}$, or
• subtraction when ${\displaystyle D=1}$.

This works because when ${\displaystyle D=1}$ the ${\displaystyle A}$ input to the adder is really ${\displaystyle {\overline {A}}}$ and the carry in is ${\displaystyle 1}$. Adding ${\displaystyle B}$ to ${\displaystyle {\overline {A}}}$ and ${\displaystyle 1}$ yields the desired subtraction of ${\displaystyle B-A}$.

A way you can mark number ${\displaystyle A}$ as positive or negative without using a multiplexer on each bit is to use a XOR (Exclusive OR) gate to precede each bit instead.

• First input to the XOR gate is the actual input bit
• Second input to the XOR gate for each is the control input ${\displaystyle D}$

This produces the same truth table for the bit arriving at the adder as the multiplexer solution does since the XOR gate output will be what the input bit is when ${\displaystyle D=0}$ and the inverted input bit when ${\displaystyle D=1}$.

## Role in the arithmetic logic unit

Adders are a part of the core of an arithmetic logic unit (ALU). The control unit decides which operations an ALU should perform (based on the op code being executed) and sets the ALU operation. The ${\displaystyle D}$ input to the adder–subtractor above would be one such control line from the control unit.

The adder–subtractor above could easily be extended to include more functions. For example, a 2-to-1 multiplexer could be introduced on each ${\displaystyle B_{i}}$ that would switch between zero and ${\displaystyle B_{i}}$; this could be used (in conjunction with ${\displaystyle D=1}$) to yield the two's complement of ${\displaystyle A}$ since ${\displaystyle -A={\overline {A}}+1}$.

A further step would be to change the 2-to-1 mux on ${\displaystyle A}$ to a 4-to-1 with the third input being zero, then replicating this on ${\displaystyle B_{i}}$ thus yielding the following output functions:

• ${\displaystyle 0}$ (with the both ${\displaystyle A_{i}}$ and ${\displaystyle B_{i}}$ input set to zero and ${\displaystyle D=0}$)
• ${\displaystyle 1}$ (with the both ${\displaystyle A_{i}}$ and ${\displaystyle B_{i}}$ input set to zero and ${\displaystyle D=1}$)
• ${\displaystyle A}$ (with the ${\displaystyle B_{i}}$ input set to zero)
• ${\displaystyle B}$ (with the ${\displaystyle A_{i}}$ input set to zero)
• ${\displaystyle A+1}$ (with the ${\displaystyle B_{i}}$ input set to zero and ${\displaystyle D=1}$)
• ${\displaystyle B+1}$ (with the ${\displaystyle A_{i}}$ input set to zero and ${\displaystyle D=1}$)
• ${\displaystyle A+B}$
• ${\displaystyle A-B}$
• ${\displaystyle B-A}$
• ${\displaystyle {\overline {A}}}$ (with ${\displaystyle A_{i}}$ set to invert; ${\displaystyle B_{i}}$ set to zero; and ${\displaystyle D=0}$)
• ${\displaystyle -A}$ (with ${\displaystyle A_{i}}$ set to invert; ${\displaystyle B_{i}}$ set to zero; and ${\displaystyle D=1}$)
• ${\displaystyle {\overline {B}}}$ (with ${\displaystyle B_{i}}$ set to invert; ${\displaystyle A_{i}}$ set to zero; and ${\displaystyle D=0}$)
• ${\displaystyle -B}$ (with ${\displaystyle B_{i}}$ set to invert; ${\displaystyle A_{i}}$ set to zero; and ${\displaystyle D=1}$)

By adding more logic in front of the adder, a single adder can be converted into much more than just an adder—an ALU.