How Logic Gates Work: OR, AND, XOR, NOR, NAND, XNOR, and NOT (2024)

By HTG Staff

Logic gate: a cool term, but what does it mean? This article will introduce the concept of a logic gate as well as describe how each specific logic gate (OR, AND, XOR, NOR, NAND, XNOR, and NOT) works.

How Logic Gates Work: OR, AND, XOR, NOR, NAND, XNOR, and NOT (1)

Quick Links

  • What Is a Logic Gate?
  • OR
  • AND
  • XOR
  • NOR
  • NAND
  • XNOR
  • NOT
  • Logic Gates in Computer Code
  • Wrapping up

Logic gate: a cool term, but what does it mean? This article will introduce the concept of a logic gate as well as describe how each specific logic gate (OR, AND, XOR, NOR, NAND, XNOR, and NOT) works.

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 <strong>NOT</strong> {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, anAND 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:

0 + 0 => 0 

0 + 1 => 1

1 + 0 => 1

1 + 1 => 1

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 alsofalse. 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:

0 + 0 => 0 

0 + 1 => 0

1 + 0 => 0

1 + 1 => 1

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:

0 + 0 => 0 

0 + 1 => 1

1 + 0 => 1

1 + 1 => 0

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:

0 + 0 => 1 

0 + 1 => 0

1 + 0 => 0

1 + 1 => 0

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:

0 + 0 => 1 

0 + 1 => 1

1 + 0 => 1

1 + 1 => 0

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.

How Logic Gates Work: OR, AND, XOR, NOR, NAND, XNOR, and NOT (2)

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:

0 + 0 => 1 

0 + 1 => 0

1 + 0 => 0

1 + 1 => 1

In other words, the reverse of aXOR 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:

0 => 1 

1 => 0

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:

if [ ! true ]; then echo 'false'; else echo 'true'; fi 
How Logic Gates Work: OR, AND, XOR, NOR, NAND, XNOR, and NOT (3)

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 anAND 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.

How Logic Gates Work: OR, AND, XOR, NOR, NAND, XNOR, and NOT (2024)

FAQs

Why XOR and XNOR are not universal gates? ›

You can't create an AND gate (or an OR gate for that matter) with XOR alone, hence it is not a universal set.

How do XOR and XNOR gates work? ›

The XOR output is asserted whenever an odd number of inputs are asserted, and the XNOR is asserted whenever an even number of inputs are asserted: the XOR is an odd detector, and the XNOR, an even detector. Xor gate can be used as a “controlled inverter”.

How does the logic gate not work? ›

A NOT gate performs logical negation on its input. In other words, if the input is true, then the output will be false. Similarly, a false input results in a true output. The truth table for a NOT gate appears to the right.

How do logic gates work? ›

A logic gate is a device performing a Boolean logic operation on one or more binary inputs and then outputs a single binary output. Computers perform more than simple Boolean logic operations on input data, and they typically output more than a single binary digit.

Why are NOR and NAND gates universal? ›

The OR, AND, and NOT are the three basic logic gates as they together can construct the logic circuit for any given Boolean expression. NOR and NAND gates have the property that they individually can be used to hardware-implement any logic circuit. For this reason, the NAND and NOR gates are called universal gates.

Why an OR gate Cannot be used as a universal gate? ›

Any composition of monotonic functions must be monotonic. Therefore, OR cannot be universal because there are non-monotonic boolean functions (like XOR or NAND.) AND is also monotonic. In fact, every monotonic boolean function can be expressed using just AND and OR.

How does XOR work in logic? ›

The XOR logic gate can be used as a one-bit adder that adds any two bits together to output one bit. For example, if we add 1 plus 1 in binary, we expect a two-bit answer, 10 (i.e. 2 in decimal). Since the trailing sum bit in this output is achieved with XOR, the preceding carry bit is calculated with an AND gate.

How is XNOR gate used in everyday life? ›

Applications of XNOR Gate

Additionally, it is used in the construction of binary adders, encoders, and decoders, which are fundamental components in digital communication and data processing systems. Arithmetic Operations: This logic gate plays a crucial role in arithmetic operations within digital systems.

What is an XOR gate for dummies? ›

In an XOR gate, the output is HIGH if one, and only one, of the inputs is HIGH. If both inputs are LOW or both are LOW, the output is LOW. Another way to explain an XOR gate is as follows: The output is HIGH if the inputs are different; if the inputs are the same, the output is LOW.

How does NAND logic gate work? ›

Known as a universal gate, NAND gates can work under the Boolean function without using any other date. In the case of NAND gates, this means the output always stays true (usually expressed as a 1) while at least one of its inputs remains false (typically expressed using a 0).

What is a real life example of a NOT gate? ›

An example of a NOT Gate would be a game of Opposites with a friend. Whatever you ask them to do, they have to do the opposite. It would go like this: - Input - You tell him to shut the door; Output - he opens the door.

Which two gates are universal? ›

A universal gate is a gate which can implement any Boolean function without need to use any other gate type. The NAND and NOR gates are universal gates.

What is the basic principle of logic gates? ›

These are the important digital devices, mainly based on the Boolean function. Logic gates are used to carry out the logical operations on single or multiple binary inputs and result in one binary output. In simple words, logic gates are the electronic circuits in a digital system.

Do logic gates exist physically? ›

Today, most logic gates are made from MOSFETs (metal–oxide–semiconductor field-effect transistors). They can also be constructed using vacuum tubes, electromagnetic relays with relay logic, fluidic logic, pneumatic logic, optics, molecules, acoustics, or even mechanical or thermal elements.

Why XOR and XNOR are called exclusive gates? ›

XOR gate (sometimes called EOR, EXOR, and pronounced as Exclusive OR) is a digital logic gate that results in true (either 1 or HIGH) output when the number of true inputs is an odd count. An XOR gate implements an exclusive OR, i.e., a true output result if one, and only one, of the gate inputs, is true.

Why is NAND and XOR called universal set of gates? ›

NAND and NOR gates are called universal gates because they can perform all the three basic logic functions OR, AND and NOT. Was this answer helpful?

Which logic gate is not the universal gate? ›

OR-AND gate defined as a + b.c, this gate is NOT universal.

Can you construct an XOR gate using only and OR NOT gates? ›

, we can construct an XOR gate circuit directly using AND, OR and NOT gates. However, this approach requires five gates of three different kinds.

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6244

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.