Logic Gates

Learn how AND, OR, NOT, XOR, NAND and NOR gates turn 1s and 0s into decisions, and how to find the output of a small circuit.

Advanced25 minLesson

Definition

A logic gate is a tiny electronic circuit with one or two inputs and one output. Every input and every output is either 1 or 0:
  • 1 = voltage on (high), which means true
  • 0 = voltage off (low), which means false
Each gate performs one Boolean operation. Gates are the hardware version of AND, OR and NOT from the earlier lessons.
The two-input gates (inputs AA and BB, rows start with 0,00, 0):
AABBANDORXORNANDNOR
0000011
0101110
1001110
1111000
The NOT gate (also called an inverter) has only one input and flips it: input 0 gives output 1, input 1 gives output 0.
How to remember them:
  • AND: output 1 only when both inputs are 1
  • OR: output 1 when at least one input is 1
  • XOR (exclusive OR): output 1 when the inputs are different
  • NAND (NOT AND): the opposite of AND, output 0 only when both inputs are 1
  • NOR (NOT OR): the opposite of OR, output 1 only when both inputs are 0
Notation: engineers often write A⋅BA \cdot B for AND, A+BA + B for OR and A‾\overline{A} for NOT. They mean exactly the same as A∧BA \land B, A∨BA \lor B and ¬A\neg A. XOR is written A⊕BA \oplus B, NAND is A⋅B‾\overline{A \cdot B} and NOR is A+B‾\overline{A + B}. Careful: here 1+1=11 + 1 = 1, because ++ means OR.
A circuit connects gates: the output of one gate becomes an input of the next. To find the final output, go from the inputs to the output, one gate at a time.
The interactive table in this lesson uses AA and BB for the inputs and 1, 0 for the values. It draws the symbol of every gate, NAND and NOR included.

Try it now

Which gate gives output 1 only when both inputs are 1?

Worked Examples

The inputs are A=1A = 1 and B=0B = 0. Find the output of an AND, OR, XOR, NAND and NOR gate.

1

AND gate

AND needs both inputs to be 1, but B=0B = 0 → AND: 00

2

OR gate

OR needs at least one 1, and A=1A = 1 → OR: 11

3

XOR gate

The inputs 11 and 00 are different → XOR: 11

4

NAND gate

NAND is the opposite of AND: AND gave 00, so flip it → NAND: 11

5

NOR gate

NOR is the opposite of OR: OR gave 11, so flip it → NOR: 00

Common Mistakes

Thinking an OR gate gives 00 when both inputs are 11

Why it's wrong: That is the XOR gate. OR is 1 when at least one input is 1, and that includes the case where both are 1.

Correct: OR: 1∨1=11 \lor 1 = 1. XOR: 1⊕1=01 \oplus 1 = 0. Only XOR asks for the inputs to be different.

Reading A+BA + B as ordinary addition and writing 1+1=21 + 1 = 2

Why it's wrong: In gate notation ++ means OR, and the output of a gate can only be 0 or 1.

Correct: With gates, 1+1=11 + 1 = 1 (OR). The value 2 only appears when you add binary numbers, and then it is written as 1010 with a carry digit.

Treating NAND as NOT A AND NOT B, that is A‾⋅B‾\overline{A} \cdot \overline{B}

Why it's wrong: NAND flips the output of AND, not the inputs. For A=1A = 1, B=0B = 0: NAND gives 1⋅0‾=1\overline{1 \cdot 0} = 1, but 1‾⋅0‾=0⋅1=0\overline{1} \cdot \overline{0} = 0 \cdot 1 = 0.

Correct: NAND =A⋅B‾= \overline{A \cdot B}: first AND, then NOT. The bar covers the whole product.

Feeding the original inputs into the last gate of a circuit

Why it's wrong: In a circuit each gate only receives the wires connected to it. The last gate usually gets the outputs of earlier gates, not A, B and C.

Correct: Work from the inputs to the output, write the output of every gate next to it, and use those values for the next gate.

Interactive Visual

Truth Table

A ∧ B
ABA ∧ B
000
010
100
111

1= True0= False

Interactive Sandbox

Expression Calculator

Try these:

History

No calculations yet

Practice Problems

18 problems
Problem 1 of 18
Easy

Which gate gives output 1 only when both inputs are 1?

Why It Matters

Every digital device you own is built from logic gates:
  • Computer chips: a processor contains billions of transistors wired together as gates, and everything it does (adding, comparing, storing) comes down to AND, OR and NOT on 1s and 0s
  • Adding numbers: two gates, an XOR and an AND, are enough to add two binary digits, and chains of such circuits add whole numbers
  • Alarms and safety systems: an alarm rings when it is armed AND a door OR a window is open
  • Cars and machines: a seat belt warning lights up when the engine is on AND the belt is NOT fastened; a press only works when both hands push two buttons (AND)
  • Lights at home: a light that can be switched from two places (stairs, hallway) is an XOR circuit
In the previous lessons you wrote Boolean expressions on paper. Logic gates are the same expressions built in hardware, so learning to read a circuit is learning to read the language of computers.

Real World Applications

Alarm Systems

A home alarm is a small logic circuit: sensors give 1 or 0, gates decide whether the siren gets 1.

Example:

With armed switch AA, door sensor DD and window sensor WW, the alarm rings when Y=A∧(D∨W)=1Y = A \land (D \lor W) = 1.

1Try It Yourself

An alarm uses Y=A∧(D∨W)Y = A \land (D \lor W): A=1A = 1 when the system is armed, D=1D = 1 when the door is open, W=1W = 1 when the window is open.

In how many of all possible input combinations does the alarm ring?

Step 1: Write the mathematical expression

Write the number of input combinations as a power of 2:

Adding Numbers in a Chip

Every addition a computer does is built from half adders and full adders made of gates.

Example:

A half adder computes the sum digit S=A⊕BS = A \oplus B and the carry digit C=A∧BC = A \land B of two binary digits.

2Try It Yourself

A half adder gets the inputs A=1A = 1 and B=1B = 1. It outputs S=A⊕BS = A \oplus B and C=A∧BC = A \land B, and the result is read as the binary number CSCS.

What are S and C, and which ordinary number does the result stand for?

Step 1: Write the mathematical expression

Write the addition the circuit performs:

Building Chips from NAND Gates

Chip makers like NAND gates because every other gate can be built from NAND gates alone.

Example:

Connect both inputs of a NAND gate to the same signal AA: the output is A⋅A‾=A‾\overline{A \cdot A} = \overline{A}, so it works as a NOT gate.

3Try It Yourself

A small chip package holds 4 NAND gates. A NOT gate needs 1 NAND gate, and an AND gate needs 2 (a NAND followed by a NAND used as NOT). A designer needs 3 AND gates and 2 NOT gates.

How many NAND gates and how many chip packages does the designer need?

Step 1: Write the mathematical expression

Write the number of NAND gates as one calculation:

Key Takeaways

  • 1A logic gate takes inputs that are 1 (voltage on) or 0 (voltage off) and gives one output, also 1 or 0
  • 2AND gives 1 only when both inputs are 1; OR gives 1 when at least one input is 1; NOT flips its single input
  • 3XOR gives 1 when the inputs are different; NAND and NOR are the opposites of AND and OR
  • 4Gate notation: A⋅BA \cdot B (AND), A+BA + B (OR), A‾\overline{A} (NOT), A⊕BA \oplus B (XOR), and here 1+1=11 + 1 = 1
  • 5To find the output of a circuit, go from the inputs to the output and write down the output of every gate
  • 6Name each gate's output as an expression to get the Boolean expression of the whole circuit

Frequently Asked Questions

Inside a chip there are only voltages. A high voltage stands for 1 (true) and a low voltage for 0 (false). Using just two levels makes circuits fast and reliable, because a signal that is a little weaker still clearly reads as 1.
Inside a chip there are only voltages. A high voltage stands for 1 (true) and a low voltage for 0 (false). Using just two levels makes circuits fast and reliable, because a signal that is a little weaker still clearly reads as 1.
They differ in only one row: when both inputs are 1. OR gives 11 (at least one is on), XOR gives 00 (the inputs are not different). XOR is the either-or-but-not-both gate.
Any other gate can be built from NAND gates alone. For example, a NAND with both inputs joined is a NOT gate. So a factory can make chips from one kind of gate. You will see the algebra behind this in the Boolean Algebra lesson.
Yes, with 0 and 1: AND is A×BA \times B, NOT is 1−A1 - A, OR is A+B−A×BA + B - A \times B and XOR is A+B−2×A×BA + B - 2 \times A \times B. For example, OR for 1,11, 1 is 1+1−1=11 + 1 - 1 = 1. Try it in the calculator.

Glossary

Logic gate
An electronic circuit that performs one Boolean operation on inputs of 1 and 0
Input / output
The signals going into a gate and the single signal coming out of it
Circuit
Gates connected by wires, where the output of one gate is an input of another
Inverter
Another name for the NOT gate, which flips 1 to 0 and 0 to 1
XOR (exclusive OR)
A gate whose output is 1 when its inputs are different, written A⊕BA \oplus B
Half adder
A circuit of one XOR and one AND gate that adds two binary digits: S=A⊕BS = A \oplus B, C=A∧BC = A \land B

Formula Card

AND

Y=A⋅B=A∧BY = A \cdot B = A \land B

1 only when both inputs are 1

OR

Y=A+B=A∨BY = A + B = A \lor B

1 when at least one input is 1

NOT

Y=A‾=¬AY = \overline{A} = \neg A

Flips the input

XOR

Y=A⊕BY = A \oplus B

1 when the inputs are different

NAND

Y=A⋅B‾Y = \overline{A \cdot B}

Opposite of AND: 0 only when both inputs are 1

NOR

Y=A+B‾Y = \overline{A + B}

Opposite of OR: 1 only when both inputs are 0

More in This Topic