Seongyeol Yi

Now You Can Build Calculators (For Real)

So far, we’ve learned that all information in the world can be represented using just the simple signals 0 and 1. We’ve also understood that by combining logic gates, we can create new information from information represented as 0s and 1s.

Now it’s time to move beyond theory and meet concrete examples. Today, we’ll combine logic gates to create circuits that actually perform addition.

calculator

Smartphone calculator apps perform addition much faster than humans. They can instantly calculate operations like 999,999 + 888,888. But the core of that calculator is ultimately just combinations of the logic gates we’ve been learning about.

After completing this post, you’ll be able to combine logic gates to create your own calculator. And it will be much faster and more accurate than calculating by hand!

Elementary School Review

Before learning how computers perform addition, let’s reflect on how we usually do addition. For example, how do you calculate 123 + 456?

  123
+ 456
-----
  579

We naturally calculate one digit at a time from right to left. If a digit produces a result greater than 10, we carry over to the next digit.

Let’s look at a more complex example with carrying: 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 at this process carefully, there are two key elements:

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

Representing Numbers with 0s and 1s

In daily life, we use the decimal system, which uses 10 numbers as basic units. In decimal, we create the entire number by multiplying each digit by values that increase by 10 times: 1, 10, 100, 1000, etc. For example, if we break down the number 123 in detail:

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

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

Computers actually use the same principle. They just use only two numbers, 0 and 1, when representing numbers. So as positions increase, we multiply by values that increase by 2 times rather than 10 times. This is called the binary system.

For example, converting the binary number 101₂ to decimal works like this:

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

Try converting decimal numbers to binary below.

Decimal to Binary Converter
Enter a decimal number to see its binary representation and calculation process.
1012
= 1 × 22 + 1 × 20
= 4+1=510

By processing numbers represented in binary with logic gates, we can create new meaningful numbers.

Trying Addition with Binary Numbers

Let’s start with a simple example.

Converting decimal 3₁₀ + 1₁₀ = 4₁₀ to binary calculation gives us 11₂ + 01₂ = 100₂.

  11   <-- (carry)
   11
+  01
-----
  100
  1. 1 + 1 equals 2₁₀ in decimal, but in binary it’s 10₂. So the current digit becomes 0 and we carry 1 to the next digit.
  2. 1(carry) + 1 + 0 = 10₂, so the current digit is 0 and we carry 1 to the next digit.

The result is 100₂, which equals decimal 4.

The key in this process is the same two results that occur in each digit position as in decimal:

Half Adder

Now let’s seriously build an addition circuit. Let’s think about the simplest case of adding two single-digit binary numbers.

Listing all possible cases:

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = ?

The last case is interesting. 1 + 1 becomes 10₂. That is, the current digit result (Sum) is 0 and a carry of 1 occurs to the next digit.

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

Truth Table
SumCarry
0000
0110
1010
1101

Implementing the above truth table with logic gates creates a calculator that can perform single-digit binary addition!

We’ve already created a single-digit addition calculator! This is called a half adder.

Half Adder Implemented Directly with NAND

Referring to the content from NAND is All You Need, implementing the above circuit with NAND gates looks like this. The top output is Carry, the bottom output is Sum.

Full Adder

Why is it called a half adder? The half adder is excellent, but it has one problem. When actually adding multi-digit numbers, we need to handle the carry value from the lower digit as well.

Let’s look at the example we calculated earlier:

  1    (carry)
  11   (decimal 3)
+ 01   (decimal 1)
----
 100   (decimal 4)

When calculating the first digit, we only needed to consider two inputs, but from the next digit onward, we need to handle the carry value from the previous digit. In the above example, we calculated 1+1+0 for the 2’s place.

We need a circuit that can handle all three input values (A, B, and the Carry from the lower digit). This is called a full adder.

The truth table for a full adder is as follows. For distinction, we’ll call the carry from the previous digit Cin, and the carry to the next digit Cout.

Truth Table
CoutSum
00000
00101
01001
01110
10001
10110
11010
11111

It looks complex, but think of each row as binary addition. For example, the last row is 1 + 1 + 1 = 11₂, so Sum is 1 and Cout is 1.

A full adder can be built using half adders and an OR gate.

Multi-Digit Addition

One full adder can only perform 1-bit addition. So how do we add multi-digit numbers like 4-bit or 8-bit?

Since the addition method doesn’t change when the number of digits changes, we simply connect multiple full adders in sequence! We place one full adder for each digit position and connect each digit’s Carry-out to the next digit’s Carry-in.

With 10 full adders, we can add numbers over 1000, and with 30, we can add numbers over a billion! In the example below, we calculate 1112 + 12 = 10002.

This method is called a ripple carry adder. It’s named this way because the carry propagates (ripples) to the next digit in sequence.

Combinational Logic

Finally, we’ve created circuits that actually perform number addition by combining logic gates!

By the way, not only addition but also subtraction and multiplication can all be built with combinations of logic gates.

The adders we created here are representative examples of Combinational Logic. Combinational logic circuits are circuits where output is immediately determined based only on current input values. That is, past inputs or states have no influence at all; results are produced based solely on the currently given input values.

In the case of adders, they receive two bits (or three bits, including Cin in full adders) as input and immediately output Sum and Carry. When input changes, output changes immediately, and they can’t remember what inputs were received previously. Combinational logic can be seen as circuits that only calculate without memory.

Conclusion

One problem remains. All the circuits we’ve built so far can’t remember calculation results. Once calculation is finished, the results disappear. To be a real computer, it must be able to store and remember information.

For example, suppose you want to calculate the 1000th Fibonacci number. The Fibonacci sequence creates the next value by continuously adding the previous two values. If you can’t remember any previous calculation results, you’d have to recalculate from the beginning every time. That would be inefficient and make it impossible to create practical programs.

In the next post, we’ll solve this problem. We’ll create circuits that store information using logic gates and understand the basic principles of memory. Let’s witness the moment when computers finally gain ‘memory’!

You need to login to leave a comment.