Logic gates explained
A computer does everything it does, from adding two numbers to showing this page, with signals that know only two values: 0 and 1. The parts that work with those signals are logic gates, and there are only a handful of kinds. What makes a processor powerful is not the gates but how many of them there are and how they are wired. This page goes from a single gate to circuits that add, choose and remember.
Zeros and ones
In a digital circuit a wire carries one of two voltages: low, which stands for 0, or high, which stands for 1. Two values are easy to tell apart even when the signal is noisy, and that is why digital electronics is so reliable. The rules for reasoning with two values are much older than the electronics. George Boole wrote them down in 1854 as an algebra of true and false, and in 1937 Claude Shannon showed in his master's thesis that circuits of switches obey exactly that algebra. Since then, designing a digital circuit and writing a formula of logic have been two views of the same thing.
The three basic gates
An AND gate has two inputs and gives 1 only when both are 1: a lamp behind two switches in a row. An OR gate gives 1 when at least one input is 1: two switches side by side. A NOT gate, or inverter, has one input and gives the opposite: 1 for 0 and 0 for 1. Every gate can be described completely by its truth table, the list of its output for every combination of inputs: four rows for a gate with two inputs, two rows for the inverter. Everything else in digital logic can be built from these three.
NAND, NOR, XOR and XNOR
Put an inverter behind an AND gate and you get NAND, which gives 0 only when both inputs are 1; in the same way OR becomes NOR. They look like mere variations, but each of them is universal: every other gate, and so every digital circuit, can be built from NAND gates alone, or from NOR gates alone. An inverter is a NAND with its two inputs tied together, an AND is a NAND followed by such an inverter, and four NAND gates make an XOR. Chip makers like NAND because it is also the cheaper gate: in the usual CMOS technology a NAND needs four transistors and an AND six. XOR, the exclusive or, gives 1 when its inputs differ, and XNOR when they are equal.
Adding with gates
Adding two bits gives 0, 1 or 2, which in binary is 00, 01 or 10. The right-hand digit, the sum, is 1 when exactly one of the bits is 1: an XOR. The left-hand digit, the carry, is 1 when both are: an AND. Those two gates together are a half adder. To add longer numbers every column must also take the carry of the column to its right. That is a full adder: two half adders and an OR gate, five gates in all. Put one full adder in a row for every bit and the carry ripples from right to left, just as when you add on paper.
Choosing and decoding
A multiplexer is a switch made of gates: a select input decides which of two data inputs reaches the output. It takes one inverter, two AND gates and an OR gate, and it is how a processor picks which value goes where. A decoder does the reverse kind of choosing: it turns a binary number into one active line out of many. With two inputs it has four outputs, of which exactly one is 1. Decoders pick the memory cell that an address refers to, and the operation that an instruction code asks for.
Circuits that remember
All the circuits so far are combinational: the outputs depend only on the inputs at that moment. Something new happens when an output is wired back to an earlier input. Take two NOR gates and feed the output of each into the other. A short 1 on the set input makes the output 1, and it stays 1 after the input has gone back to 0; a short 1 on the reset input makes it 0 again. This SR latch stores one bit. A D latch adds gates in front so that the stored bit simply follows a data input while an enable input is 1. Two D latches in a row, opened in turn by a clock, make a flip-flop, which takes a new value only at the instant the clock rises. Registers, counters and memory are rows of flip-flops.
Time, and circuits that never rest
A real gate needs a moment, far less than a billionth of a second, to answer. In a long chain those delays add up, and the number of gates on the longest path from an input to an output, the depth of the circuit, decides how fast it can be clocked: the clock may not tick again before the slowest signal has arrived. Delay also explains a curious circuit. Wire three inverters in a ring and no value can stay put, because one trip round the ring turns it into its opposite. The ring oscillates, and such ring oscillators are really used to make clock signals and to measure how fast a chip is.
Two ways to draw a gate
Circuit diagrams use two families of symbols. The ANSI symbols, the usual ones in the United States and in most textbooks, give each gate its own outline: a flat back and a round front for AND, a curved back and a pointed front for OR, the same with a second curve for XOR, and a triangle for NOT. The IEC symbols, laid down in an international standard and common in Europe, draw every gate as a rectangle with a sign inside: & for AND, ≥1 for OR, =1 for XOR and 1 for NOT. Both mark an inverted output with a small circle.
Explore it
Open the logic circuit simulator and start with the half adder: switch the two inputs and follow the green wires to the sum and the carry. Then load the full adder, the multiplexer and the XOR made of NAND gates, and compare their truth tables and formulas. Try the SR latch to see a circuit remember, the flip-flop to see it wait for the clock, and the ring oscillator to see one that cannot make up its mind. After that, clear the board and build your own.
Frequently asked questions
Why is NAND called a universal gate?
Because any logic function can be built from NAND gates alone. A NAND with both inputs tied together is an inverter; a NAND followed by that inverter is an AND; and inverting both inputs of a NAND gives an OR. With AND, OR and NOT you can write any truth table, so NAND can too. The same holds for NOR.
What is the difference between combinational and sequential logic?
In a combinational circuit the outputs depend only on the present inputs, so its truth table describes it completely: adders, multiplexers and decoders are combinational. A sequential circuit has feedback and therefore a state: its outputs also depend on what happened before. Latches, flip-flops, counters and memory are sequential, and they usually move from state to state on the tick of a clock.
How many gates are in a real processor?
The first microprocessor, the Intel 4004 of 1971, had about 2,300 transistors, enough for a few hundred gates. A current processor has tens of billions of transistors. The gates are the same kinds as in the simulator; there are simply far more of them, and each is thousands of times smaller.
What is the forbidden state of an SR latch?
Setting and resetting at the same time. With both inputs at 1, both outputs of a NOR latch become 0, so they are no longer each other's opposite, and if both inputs are released at the same moment it is a matter of chance which side wins. Circuits are designed so that this never happens; the D latch avoids it by deriving set and reset from a single data input.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open the logic circuit simulator