next up previous
Next: Incrementing Integers Up: Integer Representation and Arithmetic Previous: Comparison of Integers

Addition of Integers (Ripple Carry)

Addition in binary is very similar to addition in decimal. We can perform the traditional grade school algorithm to add with the main difference being that we carry with two instead of ten. For example,

Carry 1 1 1 0 1 1 1 0  
Input X   0 1 1 0 0 1 1 0
Input Y   1 0 1 0 1 1 1 1
Output 1 0 0 0 1 0 1 0 1

More generally,

Carry $carry_{n-1}$ $carry_{n-2}$ $carry_{n-3}$ ... $carry(2)$ $carry(1)$ $carry(0)$  
Input X   $x_{n-1}$ $x_{n-2}$ ... $x_3$ $x_2$ $x_1$ $x_0$
Input Y   $y_{n-1}$ $y_{n-2}$ ... $y_3$ $y_2$ $y_1$ $y_0$
One   0 0 ... 0 0 0 1
Output $out_n$ $out_{n-1}$ $out_{n-2}$ ... $out_3$ $out_2$ $out_1$ $out_0$

Each bit of the output except the first is from the sum of three bits - the two corresponding bits of the inputs plus the carry bit of the previous set of bits. The first bit of the output does not use a carry bit and is slightly simpler than the general case. The sum of these three bits is a two bit number from zero to three. The low order bit of this sum is the output bit while the high order bit is the carry bit.

$x_i$ $y_i$ $carry_{i-1}$ $carry_i$ $output_i$
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1

Note that the carry bit generated is the majority of the inputs and the output generated is the odd parity of the inputs (AND and XOR when there is no carry input). The combination of these two functions (i.e. three input circuit with two outputs) is called a $1$-bit adder. By building $n$ $1$-bit adders and connecting the carry bit wires appropriately, we can build an $n$-bit adder. Such an adder is called a ripple-carry adder.

\begin{eqnarray*}
output_0 & = & \mathit{parity}(x_0, y_0) \\
carry_0 & = & x_0...
...) \\
carry_i & = & \mathit{majority}(x_i, y_i, carry_{i-1}) \\
\end{eqnarray*}



Similar ideas can be used to implement subtraction. However, we will delay discussion of subtraction until after signed integers are discussed. Once signs and thus negation are available, subtraction will be reduced to negation and addition.


next up previous
Next: Incrementing Integers Up: Integer Representation and Arithmetic Previous: Comparison of Integers
Jeffrey Considine 2001-05-01