# An Adding Machine: From electricity to arithmetic.

## Introduction

In this article, you will learn how computers add numbers from Logic Gates.

How from the “True AND True” or “False OR True” of Logic Gates, computers calculate 723+128.

We will create an electrical circuit that can add numbers from “0 + 0” up to “15 + 15”. We use a small number to simplify the schemas presented. Yet, the structure to compute larger numbers is the same.

### Requirements

If you don’t know what a Logic Gate is, I suggest you read that article before this one.

## Binary numbers

Computers use binary numbers, not the decimal system. Binary numbers are easier to manage with electricity. To add up to fifteen, we need four binary digits––binary digits are also called bits–– because fifteen is “1111” in binary.

Our calculator adds “0100 + 0011” not “4 + 3”.

Check below, the board of the adding machine that we will develop—two rows of switches and one row of lightbulbs. When the lightbulb is on, it refers to 1; when off, it refers to 0.

The board above is calculating “0100 + 0011” ––or 4 + 3 in decimal––. The result is 00111, seven in decimal.

## Sum the digits

How do we add numbers? For example:

If you remember from school, adding numbers with multiple digits was done first by adding the digits alone. So first, we do 2 + 5, then 3 + 4, and we get the final result of 77.

In binary, we do the same.

Again, we do "0 + 1" first, then "1 + 0", and we get the final result of "11_{bin}" (3 in binary).

In binary and decimal bases, we also need to be careful with the carry. The carry is a digit that is moved from one column to another because the result of the sum is greater than one digit.

In decimal, we have a carry when the result is greater than ten, for example: “7 + 5 = 12”. In the previous case, we write “2,” and a “1” is passed as the carry to the next column.

In binary, there is carry when the result of adding two digits is greater than two: “1_{bin} + 1_{bin} = 10_{bin}”. In this case, we write “0,” and a one is passed as the carry to the next column.

To build our calculator, we will need to build chips that can perform these calculations with one digit. Our calculator does a very similar calculation when we add numbers manually. It adds two digits at a time and keeps track of the carry.

## Half Adder

The first chip we need is called the Half Adder. It’s a chip that can add two binary digits and return the Sum and the Carry.

The truth table for the Half Adder is:

x | y | Carry | Sum |
---|---|---|---|

0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 |

1 | 0 | 0 | 1 |

1 | 1 | 1 | 0 |

*Note that "x" and "y" are inputs of the chip: current coming in. Whereas "Carry" and "Sum" are output: current flowing out.*

If we separate the two outputs, we can identify the Carry as the AND Logic Gate: only 1 when both inputs are 1.

x | y | Carry |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

The Sum output is also a logic gate: the XOR Logic Gate.

x | y | Sum |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

The XOR or Exclusive OR is a logic gate whose output is 1 only when one of the inputs is 1, but not both.

We combine an AND and an XOR to create the Half Adder chip.

## Full Adder

The Full Adder chip is similar to the Half Adder; yet, it adds three digits instead of two. One of these three digits is called the carry because it comes from the carry output from another chip.

It can add up to “1 + 1 + 1”, the result of which is "11_{bin}"––in binary and three in decimal––. The chip only needs two outputs like the Half Adder: the Sum and the Carry.

We can combine two Half Adders and an OR to come up with the Full Adder chip.

Both the Half and Full Adders add numbers of one digit and output the Sum and the Carry separately. Let’s combine the chips to come up with the final adding machine.

## Adding Machine

We start connecting the first two digits into a Half Adder. The Half Adder outputs the Sum and the Carry; the Sum goes to the lightbulb.

The second digits are combined in a Full Adder, also connected to the Carry from the previous chip.

The last two digits follow the same pattern: both switches are connected to a Full Adder hooked to the carry of the previous chip.

In this schema, there are one Half Adder and three Full Adders.

### More digits

To develop an adding machine with more digits, we need to join more full adders. For example, an adding machine for sixteen bits would use one Half Adder and fifteen Full Adders (without counting the Half Adders inside the Full Adders).

### Performance

This design is called a *ripple carry*. The last Adder needs to wait for all the previous adders to perform any work. Current computers don’t use this design to add numbers. Rather, they use the *look-ahead carry* design that speeds up this process.

## Electricity to arithmetic

We built a circuit that starts with electricity and ends up doing mathematical calculations. *Not bad*. If you think about it, this is the whole point of computers: to use electrical signals to generate data and manipulate it.

### Thanks for reading, don't be a stranger 👋

GIMTEC is the newsletter I wish I had earlier in my software engineering career.

Every other Wednesday, I share an article on a topic that you won't learn at work.

Join more than 3,000 subscribers below.