Logic gates (or just gates) are one of most basic building blocks for computers and all things digital.
Like most things in electronics, I could write a large book concerning all things logic gate-related. Some of these things include truth tables, Boolean algebra, combinational logic, DeMorgan’s theorems and more.
This tutorial on logic gates is meant to be an introduction to the various gates and how they work. As such, we will cover some aspects of some of the above topics while reserving others for future, non-introductory posts.
The article aims primarily at the beginner who knows some basics of electronics but isn’t sure what a gate is or maybe hasn’t even heard of one yet.
However, even if you’re not a beginner you may find this post to be a good review on the basics of various logic gates and what they do.
Logic Gates: Some Basics
To properly discuss logic gates, we first need to understand truth tables.
A truth table is simply a list of all possible input values to a logic gate (or really any digital circuit) and the output response for each input combination. We usually list the inputs in ascending binary order.
Sound confusing? Let’s go through the picture below, which is a truth table for a 2-input AND gate. We’ll discuss the details of what an AND gate is and how it works shortly.
Figure 1: AND gate truth table
Because 22 = 4, there are four possible input combinations for a 2-input AND gate. These are listed in the input columns of the table. Usually, the inputs are denoted with letters, so we have input A and input B.
The output is usually denoted with the letter Y. In the table above, we can see whether the output is HIGH (1) or LOW (0) for each of the four inputs. Note that since digital circuits speak in binary, the inputs and outputs can only consist of ones and zeros.
If we had a 3-inpout AND gate the table could be extended. This time there’d be eight rows instead of four because 23 = 8, with each combination corresponding to binary numbers 000 (decimal 0) to 111 (decimal 7). The third input would be labeled C.
The Simplest of Logic Gates
The simplest logic gate technically isn’t really a gate at all though some refer to it as a NOT gate. Nonetheless, it is a very important part of digital design.
An inverter simply takes one input (either a 1 or a 0) and inverts the signal, outputting the opposite level, or the complement of the input. I won’t insult your intelligence by drawing a truth table for this but just to drive the point home if I were to put a 1 or a HIGH on an inverter it’s output would be a 0 or a LOW.
The most commonly used symbol for an inverter is below.
Figure 2: schematic symbol for an inverter. The input is A and the output is Y.
Logic Gates: Are You Active?
Notice the bubble on the output of the inverter in figure 2. This bubble represents inversion.
Bubbles on the inputs and outputs of gates also represent the gate’s active level.
The active level is the logic level defined as the ON state for a particular circuit input or output. This level is either HIGH or LOW.
An active LOW terminal is ON when it is in the logic LOW state (0), indicated by the bubble.
An active HIGH terminal is considered to be ON when it is in the logic HIGH state (1), indicated by the absence of a bubble.
In other words, if there is no bubble on the input or output we assume an active HIGH.
A quick note on positive vs. negative logic is in order here. Back in the day, some applications assumed a HIGH was a 0 and a LOW was a 1. This was called negative logic. This kind of logic turns an AND gate into an OR gate and vice-versa (from the prospective of positive logic). This was confusing.
Today, you’re unlikely to see this and positive logic where a HIGH is a 1 and a LOW is a 0 is the dominant convention. Because of this, we shall say no more about negative logic. Just be aware that it does exist.
The Basic Logic Gates
All digital logic functions can be made by using various combinations of the three basic logic functions: NOT, AND, and OR.
As we know, NOT is the same as inversion.
The functions of logic gates can be shown in truth tables or written with Boolean functions.
Boolean algebra gets its name from the nineteenth-century mathematician George Boole. It describes the relation between the inputs and outputs of a digital circuit. The input and output values are called Boolean variables.
For example, the Boolean expression that represents an inverter is:
The way to pronounce this is Y equals NOT A or Y equals A bar. Remember that a bar over a variable represents the complement or opposite of that variable.
The Boolean expression for the AND gate is:
Y = A·B
Often, this equation is written without the dot. Either way, it means the same thing and says that Y equals A AND B. The expression is similar to algebraic multiplication and is also called the logical product.
Figure 1 shows the AND gate truth table. The AND function combines two or more input variables so that the output is HIGH only if all inputs are HIGH.
In other words, with an AND gate any LOW in, regardless of the number of inputs, means a LOW output. So, if we have a ten input AND gate and 9 inputs are HIGH but one is LOW the output will be LOW.
Because of this, an AND gate can determine when certain conditions are simultaneously true.
Another common application of this gate is to enable the passage of a signal at certain times and inhibit it at others. This is done by asserting one of the inputs on a 2-input AND gate to HIGH for the desired amount of time while some digital signal is fed into the other input. Since one input is HIGH for a period of time and the AND function’s output is HIGH when all inputs are HIGH, the original signal “passes” through the gate and appears at the output.
If we wish to stop passage of the signal, all we need to do is pull one of the two inputs LOW and the output will be LOW regardless of what the signal at the other input does.
Below, we can see the normal symbol for the AND gate.
Figure 3: AND gate schematic symbol
If we string the output of this gate into another AND gate, we can make a 3-input AND gate.
Another helpful way to think of the AND function is a set of switches in series. Only when both switches close does the lamp light up.
Figure 4: AND gate represented by switches
The symbol for the OR gate, which is another basic gate, is below. The truth table follows.
Figure 5: OR gate schematic symbol
Figure 6: OR gate truth table
The Boolean expression for the OR function is:
Y = A + B
The way to pronounce this is Y equals A OR B. You may be tempted to think that this is just like algebraic addition since ANDing is similar to multiplication. They are similar, but there are some differences.
Take a look at the truth table which is telling us that 1 + 1 = 1. This is true for ORing, but obviously not true for addition.
The 2-input OR gate has an output that is HIGH when any or both inputs are HIGH. In other words, a HIGH in means a HIGH out, regardless of the number of inputs. If we have a ten input OR gate and only one input is HIGH, the output will still be HIGH.
We can use an OR gate to build a simple alarm. By wiring a magnetic switch whose output is HIGH when opened to an OR gate, then feeding that output to the appropriate circuit, we have a simple alarm.
We can also represent the OR function as a set of switches in parallel. When both or either switch closes, the lamp lights. Only when both switches are open does the lamp remain off.
Figure 7: OR gate represented by switches
If we string the output of a 2-input OR gate into another 2-input OR gate we can make a 3-input gate, just like was the case with the AND gate.
NAND and NOR Logic Gates
We know that we can combine NOT, AND, and OR gates to make any other logic function. Special logic gates exist for several of these functions. Two of the most common are NAND and NOR. In fact, each of them can be used to create any logic function, just like the three basic gates.
NAND and NOR are contractions of NOT and AND and NOT and OR. A NAND gate is NOT an AND gate and a NOR gate is NOT an OR gate. This will make more sense in a minute.
A NAND gate is an AND gate whose output is fed into an inverter (or NOT gate). A NOR gate is an OR gate whose output is fed into an inverter.
The algebraic expression for the NAND function is:
Look at the picture of the NAND gate below. Notice the bubble on the output. The NAND gate has active HIGH inputs and an active LOW output.
The truth table is below the symbol. After inspecting the table, we can see why a NAND gate is NOT an AND gate; a LOW in yields a HIGH output. This is the opposite of the AND function where a LOW in produces a LOW out.
Figure 8: a NAND gate
Figure 9: NAND gate truth table
The algebraic expression for the NOR function is:
Look at the picture of the NOR gate below. Notice the bubble on the output. The NOR gate has active HIGH inputs and an active LOW output.
The truth table is below the symbol. After inspecting the table, we can see why a NOR gate is NOT an OR gate; a HIGH in yields a LOW output. This is the opposite of the OR function where a HIGH in produces a HIGH out.
Figure 10: a NOR gate
Figure 11: NOR gate truth table
Making a 3-input NAND or NOR gate from 2-input gates is slightly different than before. We cannot simply tie 2 NANDs or NORs together as this will produce a different logic function.
To make a multiple input NAND gate, one must start with an AND gate, then feed its output into a NAND gate.
The situation is similar with the NOR gate. To make a multiple input NOR gate, one must start with an OR gate then feed its output into a NOR gate.
One more interesting point about these logic gates is that both can become inverters if you short the inputs together.
Exclusive Logic Gates
The exclusive OR (XOR) is a special case of the OR function. It’s output (for a 2-input gate) is HIGH when one (and only one) of the inputs is HIGH. To extend what we just learned, the output of a multiple-input (more than 2-input) XOR gate is HIGH when an odd number of the inputs are HIGH.
Another way to think of an XOR gate is that the output is HIGH when the inputs are different and LOW when they’re the same. This can be useful for things such as error detection in digital communications.
The Boolean equation for the XOR function is:
Y = A ⊕ B
The symbol for the XOR gate is below with the truth table below that.
Figure 12: XOR gate
Figure 13: XOR gate truth table
The exclusive NOR (XNOR) function is the complement of the XOR function and shares some of the properties.
Its Boolean equation is:
The output of the XNOR gate is HIGH when the inputs are the same and LOW when they are different. Another name for the XNOR gate is the coincidence gate due to this property.
The gate’s symbol and truth table are below.
Figure 14: XNOR gate
Figure 15: XNOR gate truth table
Don’t Judge a Logic Gate by Its Cover
The symbols for all the gates thus far are the most common in my corner of the world. However, there are other symbols for the same gates. Depending on where you live you may see some people, books, or other publications represent the various logic gates this way. The chart below summarizes the various symbols for the different logic gates.
Figure 16: various symbols for logic gates
Notice that the short diagonal line on the outputs of the rectangular symbols in the far-right column denote an active LOW output just like the bubble on the symbols in the left column.
There’s So Much More
Wow, we’ve gone through more than 2100 words so far and I haven’t even had a chance to mention Mr. DeMorgan, go into any detail about Boolean algebra, or discuss combinational logic. Nor have I had the chance to discuss other important factors concerning logic gates such as fan out, propagation delay, power dissipation and more.
Just like all subjects electronic, volumes can be written on logic gates alone. But, since I don’t want this blog post to become a monster, we’ll pick up on those subjects in some future posts.
At least now you have some of the basics now. Can you replace anything you’re doing on an Arduino or microcontroller with a few simple gates?
I bet you can.
After all, you can kill a fly with a grenade just as well as you can with a fly swatter, but which method makes the most sense?
Until next time, comment and tell us about your favorite uses for logic gates.[thrive_leads id='2917']