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 one's complement on $A$ when $D=1$ to yield $S=B-A$

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

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

• Input 0 ($I_0$) is straight through ($A_i$)
• Input 1 ($I_1$) is negated ($\overline{A_i}$)

that has control input $D$ and the initial carry connect is also connected to $D$ then:

• When $D=0$ the modified adder performs addition
• When $D=1$ the modified adder performs subtraction

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

A way you can mark number $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 $D$

This produces the same Truth table for the bit arriving at the adder as the multiplexer solution does. As when $D=0$ the XOR Gate output will be what the input bit is set to. and when $D=1$ it will effectively invert the input bit

## Role in the arithmetic logic unit

Main article: 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 $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 $B_i$ that would switch between zero and $B_i$; this could be used (in conjunction with $D=1$) to yield the one's complement of $A$ since $-A = \overline{A}$.

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

• $0$ (with the both $A_i$ and $B_i$ input set to zero and $D=0$)
• $1$ (with the both $A_i$ and $B_i$ input set to zero and $D=1$)
• $A$ (with the $B_i$ input set to zero)
• $B$ (with the $A_i$ input set to zero)
• $A+1$ (with the $B_i$ input set to zero and $D=1$)
• $B+1$ (with the $A_i$ input set to zero and $D=1$)
• $A+B$
• $A-B$
• $B-A$
• $\overline{A}$ (with $A_i$ set to invert; $B_i$ set to zero; and $D=0$)
• $-A$ (with $A_i$ set to invert; $B_i$ set to zero; and $D=1$)
• $\overline{B}$ (with $B_i$ set to invert; $A_i$ set to zero; and $D=0$)
• $-B$ (with $B_i$ set to invert; $A_i$ set to zero; and $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.