What Is a Logic Gate?
First, it’s important to realize that logic gates take many forms. Even in our personal lives, we are constantly processing things through various logic gates. While our minds are optimized at doing so, we often do not realize the thought process in motion. However, it does take place.
For example, when sitting an exam, one might know that not answering a question will lead to a negative score for that question. If you thought this through and understood the concept, your mind has just processed a NOT gate! In other words (pseudo code): if NOT {question answered} THEN negative consequences exist.
Such logic gates form the building blocks for much of the world’s code as well as for electronics. While some logic gates are much more common (for example, an AND or OR gate is much more common than a NAND or NOR gate), all logic gates are sooner or later used to get a computer or electronic device to do exactly what’s required of it—to process data in a certain way.
With the help of multiple logic gates, we can construct workflows that to some extent resemble or follow human thinking. Let’s look at each one in detail.
OR
An OR logic gate is a very simple gate/construct that basically says, “If my first input is true, or my second input is true, or both are true, then the outcome is true also.” Note how we have two inputs and one output. This isn’t the case for all logic gates. If you take a look at the header image, you can see how all logic gates have two inputs—except for the NOT logic gate, which has one input. All gates have one output.
In other words, we can write an OR logic gate into this flowchart:
Here, 0 represents false and 1 represents true. As you can see, the only way that our output could ever be false (i.e. 0) is if both inputs were also false. In all other cases, the output of our OR gate will be true.
Interestingly, if both inputs are true, the output will also be true. This is a little offset from a human thinking about OR, as the word or is often associated with one or the other.
AND
Similar to our OR logic gate, the AND logic gate will process two inputs resulting in one output, but this time, we’re looking for both inputs to be true for the outcome to become true. In other words, our logic works like this:
All other gates (except for the NOT gate) are a little more tricky to comprehend, but stay tuned.
XOR
The XOR gate is also sometimes called EOR or EXOR. The correct lingo for an XOR gate is Exclusive OR. If you remember our previous example, we were a little surprised that true and true would still lead to true, somewhat unlike human reasoning.
Welcome to XOR (exclusive OR), which solves this problem, much in line with standard human reasoning. This logic gate works like this:
The input and output are the same as our OR gate, but this time, the input really does need to be exclusive. If the input is true and true, the output is false.
NOR
Remember our earlier NOT example? We’ve reversed things. It’s somewhat similar to the NOR gate, which is basically a NOT-OR gate where OR is of the same logic as we discussed above for the OR gate.
In other words, you might think about it like this: “Anything which is not an OR-situation (i.e. true and false mixed or true and true alike to our OR gate example, even if not immediately logical to humans) renders a true outcome, and all the rest results in a false outcome.”
This leads to the following NOR gate logic:
Armed with this knowledge, we can take a look at the NAND gate:
NAND
Akin to NOR, NAND could be read as NOT-AND, and thus, anything that’s normally an AND has to be false (i.e. NOT-AND). This leads to the following outcome:
As in each of the first three cases, a full AND (which would be true and true) isn’t present. Hence, the outcome is true (1). For the last input, true and true, a full AND is present and thus (due to the NOT component, the N in NAND), the outcome is false.
In this image, we see an SN7400N chip that has four logic gates, namely, NAND gates. Thus, a higher voltage (a true/1 state) on pins 1 and 2 (bottom left) will lead to a low voltage (likely 0V) state on pin 3 at any given time. And if one of the two or both pins (1+2) would become low voltage, pin 3 would start providing a higher voltage.
XNOR
Thinking back on the OR, NOR, and XOR gates, the XNOR gate is a combination of all of them. Basically, an Exclusive NOT-OR or Exclusive NOR gate. The logic is as follows:
In other words, the reverse of a XOR gate outcome. Yes, it can get a little complex to follow.
NOT
We already briefly introduced the NOT gate earlier with our human equivalent example. The NOT gate basically reverses whatever input is given to it. If you provide true as the input, the output will be false and vice versa. Our logic table is simple:
This gate is often used in combination with other gates.
Logic Gates in Computer Code
A simple example of a NOT gate can be seen in the following Bash code:
In this example, we basically say: if not true, then echo false, otherwise echo true. Because we use a NOT gate, the output is true even though not true is false.
As you can see, code easily gets a little confusing to read and develop when you use NOT gates, and especially so when combining them with AND or OR gates. But practice makes perfect, and seasoned developers love to use complex gate conditional statements.
In many coding languages, a logical OR gate is represented by the || idiom, and an AND logic gate is often represented by the && idiom. A NOT gate is usually represented by the ! symbol.
Wrapping up
In this article, we discussed the OR, AND, XOR, NOR, NAND, XNOR, and NOT logic gates. We also covered how logic gates mimic human thinking and how they can help us write complex pieces of programming logic in a computer program. We also had a brief look at logic gates as used in computer code.
If you enjoyed reading this article, take a look at our From 0 to F: Hexadecimal and Bits, Bytes, and Binary articles, which will help you understand how computers work internally.