Using the gates presented in the previous lesson, computer engineers can construct more complex pieces of logic. These pieces are called circuits, and they form the "brain" of a computer. In this lesson, we will learn about three important circuits: the decoder, the latch, and the adder.

Decoders are circuits for selecting based on a particular value. The simplest decoder is one that takes a single binary input (0 or 1) and selects between two options based on the input. To illustrate, consider the following example from Chris Crawford :

"Suppose, for example, that we have a wire. It can carry a 1 or a 0. Thus, one wire can represent one of two choices. We could, for example, use it to select one of two light switches that we might want to turn on. In other words, if the wire has a 0 on it, then we want to turn on light #0, and if it has a 1 on it, then we want to turn on light #1. However, there's a problem: the wire by itself can't turn on the right switch. If we hook it up directly to a light, it will turn the light on when we have a 1 on it, and turn it off when we have a 0 on it. We need a decoder. A decoder for this job might look like this: If you put a 1 into this decoder, Output #0 will have a 1 on it and Output #1 will have a 0. If you put a 0 into this decoder, then Output #0 will get a 1 and Output #1 will get a 0." Since this decoder has one binary input, it is called a 1-bit decoder.

Click on the button below to launch the circuit simulator applet [Arase 1999]. Use the applet to build the decoder illustrated in the diagram above. To test the behavior of your decoder, you will need to connect a power source, a toggle switch, and some LEDs to the circuit. The finished decoder should look similar to the picture below. Larger decoders can be built to select among a greater number of options. A 2-bit decoder, for example, has two binary inputs and four outputs while a 4-bit decoder has four binary inputs and eight outputs. Crawford  explains: "The two-bit decoder looks at two wires to select one of four possible options. If you have two wires carrying 1s or 0s, then there are four possible combinations of the 1s and 0s: 00, 01, 10, and 11. If you can read binary, you will recognize these numbers as just decimal 0, 1, 2, and 3. Three wires would give eight combinations; eight wires would give 256 combinations."

The circuit diagram below represents the logic for a 2-bit decoder. When a low voltage (represented by 0) is placed on both inputs, then a high voltage appears on Output W while the remaining outputs show a low voltage. Just like we described the behavior of gates with truth tables, we can also describe the behavior of circuits with truth tables. The truth table below describes the behavior of the two-bit decoder when a low voltage is placed on both inputs. The remaining cases are left blank as an exercise. Launch the circuit simulator applet again, and use it to fill in the remainder of the truth table. The applet will display a model of a two-bit decoder with LEDs attached to the four outputs. The inputs are controlled by the two toggle switches. Whenever an output (or input) has a high voltage, the associated LED will glow red.

Two-bit decoder Partial truth table Inputs Outputs A B W X Y Z 0 0 1 0 0 0 0 1 ? ? ? ? 1 0 ? ? ? ? 1 1 ? ? ? ?

Latches are another important circuit in computers. While decoders are used for selecting, latches are used for remembering. The circuit diagram below shows a simple latch made from two NAND gates. The NAND gate stands for "Not AND" and the behavior of this gate is exactly the opposite behavior of the AND gate. The truth table for the NAND gate is displayed on the diagram inside each gate. Notice that this gate has a small circle at the end of the gate just like the NOT gate to represent inversion. Also notice that some of the lines in the diagram cross but are not connected. Only lines with a black dot at their intersection are logically connected. Crawford  describes the behavior of the latch as follows: "Suppose that you start off with both inputs ('Remember 0' and 'Remember 1') set to 1. If you then make the 'Remember 0' wire equal to 0, then the output wire will be 0. If you make the 'Remember 1' wire 0, then the output wire will be 1. More important, after you stop making one of those wires 0, and revert to the normal state in which both wires are 1, then the output will still reflect the state that you put it into earlier." The animation below illustrates this behavior. The two push buttons represent switches which temporarily change the input from 1 to 0. Press one of the switches and trace how the change of input affects the circuit. Once the "Output" value has changed, the circuit continues to remember the new value even though the inputs have reverted to 1.

Latches like the one illustrated above provide one way of creating memory for a computer. With one latch, only a single bit can be remembered. However, by grouping latches together, much larger memories can be created. The diagram below illustrates a byte of memory created from eight latches. Because latches are extremely small, millions of them can be placed on a computer chip no larger than a dime! [Brookshear 1997]. Another circuit which is fundamental to computers is the adder. This circuit is responsible for binary addition. Before looking at the circuit, it will be helpful to review the four basic rules of binary addition. We can actually think of these four rules as a truth table with the slight exception of the fourth rule. Notice that the fourth rule causes a carry.

 Rule 1 Rule 2 Rule 3 Rule 4 0 + 0   0 0 + 1   1 1 + 0   1 1 + 1  10
 A B Output Carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 To implement an adder circuit, we can use the exclusive OR gate (XOR) which has exactly the same behavior as the "Output" column of the table for binary addition. XOR is the "either-or-but-not-both" gate. Thus, when both inputs are 1, the output is 0. To represent the carry, we can use the AND gate which has the same behavior as the "Carry" column. The diagram to the right shows how the circuit is connected. Try building this adder with the circuit simulator applet (note that the applet calls the exclusive OR gate "EOR" and not "XOR"). If you need help, you may view the answer. Like the latch, adders can be joined together to make more powerful adders. However, extra circuitry is required to handle the addition of the carry bits. Crawford  explains: "Consider this example: we want to add two eight-bit binary numbers. If we break this addition up by digits, adding just one column at a time, then we can use a one-bit adder for each column. We start at the right side of the number, just like you do in regular addition. If the one-bit adder ends up generating a carry, it passes the carry on to the next higher one-bit adder, which adds it into its work." 