Reading history

Logic Gates and Boolean Algebra

5 min read 0 views

Learn AND, OR, NOT, NAND, NOR and XOR gates with truth tables, the laws of Boolean algebra and De Morgan's laws, and how a half adder adds two bits.

Inside every phone and laptop are billions of tiny switches. Each one is either on or off, 1 or 0. On their own they can do nothing clever, but wired together into logic gates they can add numbers, compare passwords and run games. In this lesson you will learn the basic gates, their truth tables, the rules of Boolean algebra, and how a few gates can add two binary digits.

In this lesson you will learn

  • What Boolean values and logic gates are
  • The AND, OR and NOT gates and their truth tables
  • The NAND, NOR, XOR and XNOR gates
  • The main laws of Boolean algebra, including De Morgan’s laws
  • How a half adder adds two bits

You should know binary numbers.

Boolean Values

In 1854 the English mathematician George Boole described a kind of algebra that uses only two values: true and false. We write them as 1 and 0. Almost a century later, engineers saw that this algebra fits electronic switches perfectly. A high voltage means 1 and a low voltage means 0.

A logic gate is a small circuit that takes one or more input bits and gives one output bit. A truth table lists the output for every possible combination of inputs. With two inputs there are 2 × 2 = 4 rows; with three inputs there are 8.

The Three Basic Gates

  • AND (written A·B): the output is 1 only if both inputs are 1. Think of two switches in series: the bulb lights only if both are closed.
  • OR (written A + B): the output is 1 if at least one input is 1. Think of two switches in parallel.
  • NOT (written A′ or Ā): one input, and the output is the opposite. It is also called an inverter.
ABA·BAND1 only if both are 1ABA + BOR1 if at least one is 1AA′NOTflips 0 and 1
ABA AND BA OR BNOT A
00001
01011
10010
11110

NAND, NOR, XOR and XNOR

ABNANDNORXORXNOR
001101
011010
101010
110001
  • NAND is AND followed by NOT. NOR is OR followed by NOT.
  • XOR (exclusive OR, A ⊕ B) gives 1 when the inputs are different.
  • XNOR gives 1 when the inputs are the same, so it works as an equality checker.

NAND and NOR are called universal gates. Any circuit at all can be built using only NAND gates, or only NOR gates. Chip makers like this because they can mass-produce one kind of gate.

Laws of Boolean Algebra

Identity:      A + 0 = A          A · 1 = A
Null:          A + 1 = 1          A · 0 = 0
Idempotent:    A + A = A          A · A = A
Complement:    A + A′ = 1         A · A′ = 0
Double NOT:    (A′)′ = A
Commutative:   A + B = B + A      A · B = B · A
Distributive:  A · (B + C) = A·B + A·C
Absorption:    A + A·B = A        A · (A + B) = A

De Morgan’s laws

(A + B)′ = A′ · B′   and   (A · B)′ = A′ + B′. To break a NOT bar over a group, change OR to AND (or AND to OR) and NOT each part.

These laws let us simplify circuits, so fewer gates are needed:

Simplify  X = A·B + A·B′
X = A·(B + B′)      distributive law
X = A·1             complement law
X = A               identity law
Four gates (2 AND, 1 OR, 1 NOT) shrink to a plain wire.

Adding Two Bits: the Half Adder

When you add two single bits, there are four cases: 0+0 = 0, 0+1 = 1, 1+0 = 1 and 1+1 = 10 in binary. So we need two outputs: a Sum bit and a Carry bit.

A  B | Carry  Sum
0  0 |   0     0
0  1 |   0     1
1  0 |   0     1
1  1 |   1     0

Sum   = A XOR B
Carry = A AND B

One XOR gate and one AND gate make a half adder. Two half adders and an OR gate make a full adder, which also accepts a carry from the column before. Chain 64 full adders together and you have the adding unit of a 64-bit processor.

Common Mistakes

  1. Reading + as ordinary addition. In Boolean algebra, 1 + 1 = 1, because it means OR.
  2. Mixing up OR and XOR. OR gives 1 when both inputs are 1; XOR gives 0.
  3. Breaking a NOT bar wrongly. (A + B)′ is A′·B′, not A′ + B′.
  4. Missing rows in a truth table. n inputs need 2ⁿ rows. Count in binary to list them all.

Practice Exercises

Exercise 1. Find the output of (A AND B) OR (NOT C) when A = 1, B = 0, C = 0.

Show answer

A AND B = 0. NOT C = 1. 0 OR 1 = 1.

Exercise 2. Simplify A + A′·B.

Show answer

A + A′·B = (A + A′)·(A + B) = 1·(A + B) = A + B. (This uses the rule that OR distributes over AND in Boolean algebra.)

Exercise 3. A lift motor should run only if the door is closed (D = 1) and a floor button is pressed (F = 1), or if the emergency override (E = 1) is on. Write the Boolean expression.

Show answer

Motor = D·F + E.

Frequently Asked Questions

Why are NAND and NOR called universal gates?

Because any other gate, and so any digital circuit, can be built using only NAND gates or only NOR gates. For example, joining both inputs of a NAND gate makes a NOT gate.

What is the difference between OR and XOR?

Both give 1 when exactly one input is 1. When both inputs are 1, OR gives 1 but XOR gives 0. XOR is true only when the inputs are different.

Summary

  • Logic gates work on bits; a truth table shows the output for every input combination.
  • AND needs all inputs 1; OR needs at least one; NOT inverts.
  • NAND and NOR are universal; XOR is 1 when inputs differ.
  • Boolean laws and De Morgan’s laws simplify circuits; a half adder uses XOR for the sum and AND for the carry.

Next: Data Representation: Text, Images and Sound.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *