Foundation of Computers part 2 – Binary Addition

In a previous article we looked at building logic gates from transistors. The next step is to use those logic gates to perform a calculation. We’ll only be adding 1 + 1, but this is just another stepping stone to performing more complicated calculations.

 

Equipment you’ll need

To build the circuits in this article you will need the following components:

  • Breadboard: designs vary, so you may need to adjust the circuit if your breadboard is different to the one shown.
  • M74HC08B1: AND gate integrated circuit
  • HCF4070BE: XOR gate integrated circuit
  • 2 push buttons
  • 2 LEDs
  • 2 10k resistors
  • 2 1k resistors
  • Various wires
  • 5V DC power source. I have a variable voltage mains adapter.

You could use alternative integrated circuits, but check their datasheets for power requirements and their pin arrangement.

 

Integrated Circuits

You could go a long way to building an entire computer using individual transistors, and it would be impressive.  However, it would also be slightly monstrous, like this 14,000 transistor computer.  It would also be tedious to keep building the same arrangements for AND, OR, and NOT gates over and over again.

A general rule of computers (both hardware and software) is to solve a problem once, bundle it up into a convenient package, and then keep reusing it.  Integrated Circuits are an example of this for hardware.  Integrated circuits (ICs) package together functionality to create things as diverse as timers and radio frequency receivers. At the extreme end, computer processors are also integrated circuits.  A relatively modern processor like the one in the Xbox One X contains the equivalent of 7 billion transistors (7,000,000,000).

Example integrated circuits

Example integrated circuits

In this article we’ll be using integrated circuits like the ones shown above. The actual integrated circuit is much smaller, and hidden away in the middle. The majority of the chip is just convenient packaging. They come in standard sizes, with standard distances between the metal pins.  Pay attention to the semi-circular notch, which is on the left edge of the integrated circuits shown in the image above. This helps you to work out the orientation.

 

Using an AND Chip

The M74HC08B1 integrated circuit contains four AND gates.  Each of the AND gates has two inputs, and a single output – just like the the AND gate circuit in the previous article. The arrangement of the gates is shown in the diagram below (taken from the datasheet).

HCF4070BE gate arrangement

HCF4070BE gate arrangement

The breadboard layout shown below demonstrates a connection to one of the AND gates – gate 4. The inputs are 4A and 4B. The output is 4Y.  Note that you need to provide power to the integrated circuit to make it work.  For this particular integrated circuit you need to connect a 5V DC power supply to Vcc (pin 14) and GND (pin 7), as shown.

Breadboard layout using AND gate integrated circuit

The video below shows the circuit in use.  For the AND gate, both buttons need to be pressed before the LED turns on:

 

Truth Tables

Truth tables are a way of describing logic gates.  The image below shows all of the combinations of button presses, and the output that you get from the AND logic gate.

Button combinations for AND logic gate

Button combinations for AND logic gate

A truth table shows the same information, but uses zeroes and ones.

Input A Input B Output
0 0 0
0 1 0
1 0 0
1 1 1

Zeros represent no electricity flowing (button un-pushed or LED unlit), and ones represent electricity flowing (button pushed or LED lit)

 

The XOR Logic Gate

The easiest way to create our adding circuit to use an exclusive OR gate, also known as an XOR gate. As the name suggests, this type of gate is similar to an OR gate. If electricity is provided to either input, then there is an output. However, unlike an OR gate, if electricity is provided to both inputs then there is no output.

Part of the reason for introducing truth tables is that it is much easier to understand the XOR gate when presented this way:

Input A Input B Output
0 0 0
0 1 1
1 0 1
1 1 0

It might only be the bottom row that is separates the XOR from the OR gate, but it takes a bit of effort to achieve. You can’t build an XOR gate from two transistors, but in true computer fashion you can create an XOR gate by arranging some of the basic logic gates:

An XOR logic gate, assembled from two NOTs, two ANDs, and an OR.

An XOR logic gate, assembled from two NOTs, two ANDs, and an OR. You can create your own interactive diagram at https://sciencedemos.org.uk/logic_gates.php

 

Using an XOR Chip

The HCF4070BE contains four XOR gates.  Each of the XOR gates has two inputs, and a single output. The arrangement of the gates is shown in the diagram below (taken from the datasheet). The presentation is different to the diagram of the M74HC08B1 shown above, but the pin layout is actually the same.

XOR

HCF4070BE gate arrangement

The breadboard layout shown below demonstrates a connection to one of the AND gates – gate M. The inputs are H and G. You can keep exactly the same layout that you used for the previous circuit, and just swap the integrated circuit.

Breadboard layout using XOR gate integrated circuit

The video below shows the circuit in use.  For the XOR gate, the LED turns on when either button is pressed, but turns off if both buttons are pressed:

 

Adding Numbers

Computers use the binary system, where electricity is either on or off. But before we look at adding numbers in binary, lets look at adding numbers in the decimal system.

In the decimal system there are ten possible values: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. If you add 1 to a number, you simply increase the value in the column on the right. When you run out of possible numbers on the right, you “carry over” – increasing the next column to the left, and resetting the right column to zero.

Decimal addition

Decimal addition

Why did we look at decimal addition in such a convoluted way? Because binary addition follows exactly the same rules. The difference this time is that there is only two possible values: 0 and 1. However, if you are adding 1, you still increase the value on the right, and carry over when you run out of possible values.

Binary addition

Binary addition

Let’s look at it a slightly different way. The truth table below shows the possible combinations of adding two binary digits – also know as bits.

Input A Input B Carry over Sum
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

Normally if you see a table like this the sum and carry over columns are the other way round, but I’ve swapped them to match the image above.

Regardless of the order, you should notice something familiar about the carry over and sum columns. The carry over column matches the output of an AND gate, and the sum column matches the output of an XOR gate.

 

Creating a Half Adder

Time to do some binary addition with our integrated circuits. Basically we’re going to wire up an AND gate for the carry over, and an XOR gate for the sum. The breadboard diagram below shows the layout. You only need to make some small changes to the circuit that you used before. Pay special attention to the extra black wire above the XOR integrated circuit. This connects one of the input pins to ground. The reason is explained in the following section.

Breadboard circuit for Half Adder using AND gate and XOR gate integrated circuits

The video below shows the circuit in use. I swapped the push buttons for a DIP switch so that it is clearer to see when something is on or off. Each switch that is turned on represents a binary digit (bit) to be added. The green LED represents the sum, and the red LED represents the carry over.

Why is it called a half adder? As it stands we couldn’t chain these circuits together to add multiple bits. It generates a carry over, but it doesn’t accept a bit carried over from another binary addition.

 

Doing things correctly

The circuits shown above are basic. They worked for me and they should work for you, but they are not the “correct” way of wiring up the integrated circuits.

Most of the pins of the integrated circuits are not connected to anything. This is generally frowned upon in electronics. No electricity is supplied to them, but technically they don’t have a zero value unless they are connected to ground. Currently, they’re in an ambiguous state. This didn’t cause me any problems until I wired up the half-adder circuit. The green LED always had electricity flowing through it, even when no switches were on. All of these problems went away when I connected pin 1 of the XOR gate to ground. If you don’t get the results that you are expecting, try connecting the inputs of each integrated circuit to ground. The video below shows a fully grounded circuit.

Incidentally, that is the purpose of the resistors that are connected to the input pins. These resistors are called pull-down resistors.  Their purpose is to remove ambiguity by connecting the pins to ground. If no electricity is flowing, then this connection to ground ensures that they register as having a zero value. However, you couldn’t just use a wire going straight to ground. If you did, then as soon as electricity started flowing it would by-pass the whole circuit and take this easy route to ground. The resistor has to provide enough resistance so that the route through the integrated circuit and LED is the easier route.

You also might have noticed something different in the upper-right corner of the breadboard. This is a voltage regulator. The premise is that a voltage source might not be exact. Batteries in particular produce a decreasing voltage as they are drained. They might start at 5V, but could soon drop to 4.9V, then 4.8V, and so on. There are different types of voltage regulator, but their purpose is to provide a constant voltage. For example, you can connect a higher voltage, say 9V, and the voltage regulator produces a constant output of 5V. So long as the input voltage stays above 5V, the output voltage stays fixed at 5V.

Going further, I’ve seen recommendations to connect capacitors in parallel to the voltage regulator, to even out minor fluctuations in the voltage. It’s a bit excessive for this article though.

 

Summary

The purpose of this article was to show the next step from the simple AND, OR, and NOT logic gates that you can make with transistors. By arranging these simple logic gates, you can create a circuit to perform binary addition. The next step from here is to add larger numbers together.



Comments are closed.