CS with Seongyeol

A Calculator Made of Switches

So far, we’ve combined logic gates to build circuits that select and distribute signals. In this post and the next, let’s combine gates to build a circuit that actually performs addition.

calculator

The calculator app on your smartphone performs addition far faster than a human. It handles calculations like 999,999 + 888,888 in an instant. But at its core, that calculator is nothing more than a combination of the logic gates we’ve been learning about.

Decimal Addition

Before looking at how computers add, let’s revisit how we normally do addition. For example, how do we calculate 123 + 456?

  123
+ 456
-----
  579

We naturally calculate from the rightmost digit, one place at a time. If a result in one place exceeds 10, we carry it over to the next place.

Let’s look at a more complex example with carries. Calculating 179 + 287:

  11  <-- (carry)
  179
+ 287
-----
  466
  1. 9 + 7 = 16, so we write 6 and carry 1.
  2. 1 + 7 + 8 = 16, so we write 6 and carry 1.
  3. 1 + 1 + 2 = 4.

Looking closely at this process, there are two key elements:

  • The result at each place (Sum)
  • The carry to the next place (Carry)

These two are exactly the key ideas behind how computers perform addition. But computers only know 0 and 1. How can we transfer this principle to the world of 0s and 1s?

Binary

In everyday life, we use the decimal system, which uses 10 digits as its base unit. In decimal, each digit is multiplied by a value that grows by 10 times — 1, 10, 100, 1000, and so on — to form the whole number. For example, breaking down the number 123:

1 × 100 + 2 × 10 + 3 × 1
= 1 × 102 + 2 × 101 + 3 × 100

The leftmost place represents hundreds, the middle represents tens, and the last represents ones. It’s a positional notation system.

Computers actually use the same principle. They just use only two digits, 0 and 1, to represent numbers. So as places increase, values are multiplied by powers of 2 instead of 10. This is called binary.

For example, converting the binary number 1012 to decimal:

1 × 22 + 0 × 21 + 1 × 20
= 1 × 4 + 0 × 2 + 1 × 1
= 4 + 0 + 1 = 5 10

By processing numbers expressed in binary through logic gates, we can produce meaningful new numbers.

Binary Addition

Let’s start with a simple example.

Calculating 310 + 110 = 410 in binary gives us 112 + 012 = 1002.

  11   <-- (carry)
   11
+  01
-----
  100
  1. Adding 1 + 1 gives 210 in decimal, but in binary that’s 102. So the current place becomes 0 and we carry 1 to the next place.
  2. 1 (carry) + 1 + 0 = 102, so the current place is 0 and we carry 1 to the next place.

The result is 100₂, which equals decimal 4.

The key in this process, just like with decimal, is the two results at each place:

  • The current place’s result (Sum)
  • The carry to the next place (Carry)

Half Adder

Now let’s actually build an addition circuit. Consider the simplest case: adding two single-digit binary numbers.

Listing all possible cases:

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10

The last case is interesting. 1 + 1 becomes 102. That is, the current place result (Sum) is 0 and a carry of 1 goes to the next place.

Looking at the truth table, Sum is 1 only when A and B are different, and Carry is 1 only when both A and B are 1.

SumCarry
0000
0110
1010
1101

Implementing this truth table with logic gates creates a calculator that can add single-digit binary numbers. As we learned in the previous post, implementing it with only NAND gates looks like this:

This circuit is called a half adder. For brevity in later discussions, we’ll bundle it into a single block labeled HALF ADDER.

Wrap-up

A half adder can only handle the addition of two single-digit binary numbers. But in real addition, we also need to handle the carry value coming from the lower digit. In the next post, let’s build a full adder that solves this problem and extend it to multi-digit addition.