edubun
English
1 / 20

3 / 6 · Intro

Logic gates

Intel's first microprocessor, the 4004 of 1971, held 2,300 transistors. They were wired into gates, and gates are all a processor is made of.

Jump to the 10 practice questions

You will learn

  • Work out AND, OR, NOT, XOR, NAND and NOR
  • Build a rule out of gates

Before this: Bytes, hexadecimal and text

1Explain

From Boole to Shannon

In 1854 the mathematician George Boole published An Investigation of the Laws of Thought, an algebra in which statements are either true or false and are joined with and, or and not. For decades it was a topic for logicians. In 1938 Claude Shannon, then a student at MIT, published a paper drawn from his master's thesis showing that circuits of relays and switches obey exactly that algebra: switches in series act like AND, switches in parallel like OR. Designers could now work out a circuit on paper and simplify it before building it.

2Problem

Shannon's 1938 paper

What did Shannon's 1938 paper show?

Hint

Switches in series and in parallel.

Solution

Answer: Switching circuits follow Boole's algebra of true and false

Series switches act as AND and parallel switches as OR, so circuits can be designed with algebra.

3Explain

Shannon's two circuits on a desk

You can build Shannon's two circuits with a battery, a lamp and two switches. Put the switches in a row and the lamp lights only if the first and the second are both closed. Put them side by side and the lamp lights if either one is closed. These circuits already make decisions. A logic gate is a small circuit of the same kind, built from transistors instead of hand switches.

4Problem

Two switches in a row

Two switches in series with a lamp behave like which gate?

Hint

The lamp needs the first and the second closed.

Solution

Answer: AND

Either open series switch breaks the only current path, so both must close for the lamp to light, matching AND.

5Explore

Six gates and the tables that define them

AND gives 1 only when both inputs are 1, like the switches in a row. OR gives 1 when at least one input is 1, like the switches side by side. NOT has a single input and flips it, so 0 becomes 1 and 1 becomes 0. XOR, short for "exclusive or", gives 1 when the two inputs differ. NAND and NOR are AND and OR with a NOT on the end, which makes their outputs the exact opposite. A truth table lists every combination of inputs and the output for each.

Loading

Drag to turn. Scroll or pinch to zoom.

Tap A or B (or use the buttons) and watch the output lamp and the highlighted row of the truth table. Pick another gate from the menu. The last choice, Half adder, joins an XOR and an AND; the next unit uses it to add.

6Problem

AND

AND with inputs 1 and 0 gives?

Hint

Both must be 1.

Solution

Answer: 0

AND outputs 1 only when every input is 1; the input 0 breaks that condition, so the output is 0.

7Problem

OR

OR with inputs 1 and 0 gives?

Hint

At least one 1.

Solution

Answer: 1

One input is 1, which is enough for OR.

8Problem

XOR

XOR with inputs 1 and 1 gives?

Hint

1 only when the inputs differ.

Solution

Answer: 0

XOR is 1 only when the inputs differ.

9Problem

NAND

NAND with inputs 1 and 1 gives?

Hint

AND, then flip.

Solution

Answer: 0

AND gives 1, and the NOT on the end flips it to 0.

10Problem

NOR

NOR with inputs 0 and 0 gives?

Hint

OR, then flip.

Solution

Answer: 1

NOR is OR followed by NOT: neither input is 1, so OR gives 0 and NOT flips it to 1.

11Explore

Gates as operators

In JavaScript & is AND, | is OR and ^ is XOR on bits. Pick A and B and compare the lines with one row of the table.

A
B
const a = 1, b = 0;
console.log("AND  " + (a & b));
console.log("OR   " + (a | b));
console.log("XOR  " + (a ^ b));
console.log("NAND " + (1 - (a & b)));
console.log("NOR  " + (1 - (a | b)));
Output
AND  0
OR   1
XOR  1
NAND 1
NOR  0
ABANDORXORNANDNOR
0000011
0101110
1001110
1111000

12Problem

Rows for three inputs

How many rows does the truth table of a 3-input gate have?

rows

Hint

Each extra input doubles the rows.

Solution

Answer: 8 rows

Each input independently has two choices, so multiply the choices for all three inputs: 2³ = 8 rows.

13Explain

An alarm, an equality test and a password check

Gates combine into rules of any size. Take a burglar alarm. It should ring when the system is armed AND (the door is open OR a window is open). That is one OR gate feeding one AND gate. To check whether two bits are equal, put a NOT after an XOR, because XOR gives 1 exactly when the bits differ. Set eight of those side by side, join their outputs with AND, and the circuit tells you whether two whole bytes match. Every comparison a program makes, from "is the password right" to "has the timer reached zero", ends up as a circuit like this.

14Explain

Test the alarm one change at a time

Test the alarm rule by changing one condition at a time. First leave the system unarmed and open the door: the final AND must keep the alarm off. Next arm the system while both openings stay closed: the OR still gives no reason to ring. Finally open the door while armed and trace the signal through both gates. These checks catch a circuit that rings for an open door even when the system is not armed.

15Explain

Why chip makers can get by with NAND alone

NAND has a handy property: every other gate can be built from NAND gates alone. Tie both inputs of a NAND together and it becomes NOT, because NAND of 1 and 1 is 0 and NAND of 0 and 0 is 1. Put that NOT after another NAND and you have AND again. A few more make OR and XOR. Chip makers like this because they can lay down enormous numbers of one well-tested shape. In a real chip a gate is a handful of transistors. The 4004 had 2,300 of them, and Intel lists 1.16 billion for its 2010 second-generation Core processor.

16Problem

A NAND with its inputs joined

You join both inputs of a NAND gate together. What gate do you get?

Hint

NAND of 1 and 1 is 0; of 0 and 0 it is 1.

Solution

Answer: NOT

NAND(0,0) = 1 and NAND(1,1) = 0: the output is the input flipped.

17Problem

The Intel 4004

How many transistors did Intel's 4004 of 1971 have?

transistors

Hint

A few thousand.

Solution

Answer: 2300 transistors

Intel’s timeline gives 2,300 transistors for the 4004; the count is of transistors, not complete logic gates.

18Practice

The burglar alarm

Finish alarm(armed, door, window): it returns 1 when the system is armed and the door or the window is open, and 0 otherwise. Use & and |.

Expected output
0
0
1
1
Your code
function alarm(armed, door, window) {
  return 0;
}
console.log(alarm(0, 1, 0));
console.log(alarm(1, 0, 0));
console.log(alarm(1, 1, 0));
console.log(alarm(1, 0, 1));
Hint

One OR gate feeds one AND gate.

A solution
function alarm(armed, door, window) {
  return armed & (door | window);
}
console.log(alarm(0, 1, 0));
console.log(alarm(1, 0, 0));
console.log(alarm(1, 1, 0));
console.log(alarm(1, 0, 1));

19Explain

Common mistakes

  • "OR means one or the other, but not both."

    Everyday "or" often works that way: "tea or coffee?" usually means pick one. In logic that rule is XOR. OR also gives 1 when both inputs are 1.

  • "A truth table for 3 inputs has 6 rows."

    3 × 2 is the wrong count. Each extra input doubles the rows, because every earlier row appears once with the new input at 0 and once at 1. 2³ = 8. Four inputs already need 16.

  • "NAND and NOR are new kinds of switch."

    NAND is AND with a NOT on the end, and NOR is OR with a NOT on the end. Nothing new happens inside. What makes NAND special is that it alone is enough to build every other gate.

20

Summary

  • A gate turns input bits into an output bit by a fixed rule.
  • AND: both 1. OR: at least one 1. NOT: flip. XOR: inputs differ.
  • NAND and NOR are AND and OR with the output flipped; NAND alone can build every gate.
  • A truth table with n inputs has 2ⁿ rows.

Next unitHow gates add numbers
Full text · The whole lesson as one text
BackgroundIn 1854 the mathematician George Boole published An Investigation of the Laws of Thought, an algebra in which statements are either true or false and are joined with and, or and not. For decades it was a topic for logicians. In 1938 Claude Shannon, then a student at MIT, published a paper drawn from his master's thesis showing that circuits of relays and switches obey exactly that algebra: switches in series act like AND, switches in parallel like OR. Designers could now work out a circuit on paper and simplify it before building it. In December 1947, John Bardeen and Walter Brattain at Bell Labs made the first point-contact transistor, the small electronic switch that later replaced relays and vacuum tubes in computers.

Shannon's two circuits on a desk

You can build Shannon's two circuits with a battery, a lamp and two switches. Put the switches in a row and the lamp lights only if the first and the second are both closed. Put them side by side and the lamp lights if either one is closed. These circuits already make decisions. A logic gate is a small circuit of the same kind, built from transistors instead of hand switches. The basic gates here take one or two input bits and give one output bit by a fixed rule. Other gates can have more inputs.

Six gates and the tables that define them

AND gives 1 only when both inputs are 1, like the switches in a row. OR gives 1 when at least one input is 1, like the switches side by side. NOT has a single input and flips it, so 0 becomes 1 and 1 becomes 0. XOR, short for "exclusive or", gives 1 when the two inputs differ. NAND and NOR are AND and OR with a NOT on the end, which makes their outputs the exact opposite. A truth table lists every combination of inputs and the output for each. Two inputs make four rows, three make eight, and n inputs make 2 to the power n.

Tap A or B (or use the buttons) and watch the output lamp and the highlighted row of the truth table. Pick another gate from the menu. The last choice, Half adder, joins an XOR and an AND; the next unit uses it to add.
ABANDORXORNANDNOR
0000011
0101110
1001110
1111000

An alarm, an equality test and a password check

Gates combine into rules of any size. Take a burglar alarm. It should ring when the system is armed AND (the door is open OR a window is open). That is one OR gate feeding one AND gate. To check whether two bits are equal, put a NOT after an XOR, because XOR gives 1 exactly when the bits differ. Set eight of those side by side, join their outputs with AND, and the circuit tells you whether two whole bytes match. Every comparison a program makes, from "is the password right" to "has the timer reached zero", ends up as a circuit like this.

Why chip makers can get by with NAND alone

Test the alarm rule by changing one condition at a time. First leave the system unarmed and open the door: the final AND must keep the alarm off. Next arm the system while both openings stay closed: the OR still gives no reason to ring. Finally open the door while armed and trace the signal through both gates. These checks catch a circuit that rings for an open door even when the system is not armed.

NAND has a handy property: every other gate can be built from NAND gates alone. Tie both inputs of a NAND together and it becomes NOT, because NAND of 1 and 1 is 0 and NAND of 0 and 0 is 1. Put that NOT after another NAND and you have AND again. A few more make OR and XOR. Chip makers like this because they can lay down enormous numbers of one well-tested shape. In a real chip a gate is a handful of transistors. The 4004 had 2,300 of them, and Intel lists 1.16 billion for its 2010 second-generation Core processor. That is about 500,000 times as many in 39 years.

Common mistakes
  • "OR means one or the other, but not both." Everyday "or" often works that way: "tea or coffee?" usually means pick one. In logic that rule is XOR. OR also gives 1 when both inputs are 1.
  • "A truth table for 3 inputs has 6 rows." 3 × 2 is the wrong count. Each extra input doubles the rows, because every earlier row appears once with the new input at 0 and once at 1. 2³ = 8. Four inputs already need 16.
  • "NAND and NOR are new kinds of switch." NAND is AND with a NOT on the end, and NOR is OR with a NOT on the end. Nothing new happens inside. What makes NAND special is that it alone is enough to build every other gate.
Teacher's noteRead the story first, then build the two switch circuits, even if only as a drawing, before you show any gate symbols. Learners who have seen series and parallel wiring take AND and OR for granted. The pair that causes trouble is OR and XOR, because everyday "or" usually means XOR. Ask "tea or coffee?" and then "what if I say both?" When writing truth tables, insist on the 00, 01, 10, 11 row order; it is counting in binary from the last unit, and it stops rows from going missing. Building NOT from one NAND is a good five-minute puzzle to finish on.
Key points
  • A gate turns input bits into an output bit by a fixed rule.
  • AND: both 1. OR: at least one 1. NOT: flip. XOR: inputs differ.
  • NAND and NOR are AND and OR with the output flipped; NAND alone can build every gate.
  • A truth table with n inputs has 2ⁿ rows.

Practice questions from Logic gates on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (10)
  • (checked 2026-10-17)
  • Standards: NIST, "Prefixes for binary multiples" (physics.nist.gov/cuu/Units/binary.html: IEC approved kibi, mebi, gibi in December 1998; 1 B = 8 bit; 1 MiB = 1 048 576 B; 1 MB = 1 000 000 B; 1 GiB = 1 073 741 824 B; historical note: memory makers usually mean 2²⁰ bytes by megabyte, storage makers 1 000 000, and the "1.44 MB" diskette uses a megabyte of 1 024 000 bytes)
  • IETF RFC 20 "ASCII format for Network Interchange" (1969, 7-bit codes, A = 65, a = 97, 0 = 48)
  • IETF RFC 3629 "UTF-8, a transformation format of ISO 10646" (2003: 1 to 4 octets per character, ASCII unchanged, code points up to U+10FFFF, U+0800 to U+FFFF take three octets, "the first octet of a multi-octet sequence indicates the number of octets", example U+D55C U+AD6D U+C5B4 = ED 95 9C EA B5 AD EC 96 B4)
  • Official documentation: MDN Web Docs, "<color>" (hexadecimal colour notation #RRGGBB)
  • Intel, "Intel Chips" timeline poster (2012: Intel 4004 of 1971 with 2,300 transistors; 2nd generation Core 2010 with 1.16 billion)
  • Teaching model: the toy computer in the network-lab scene (16 cells of 8 bits, 4-bit opcode and 4-bit address) is made for this course, not a real chip. History blocks only (the logic-gates story and the von Neumann paragraph of the-cpu): George Boole, An Investigation of the Laws of Thought (1854, Project Gutenberg 15114)
  • Claude E. Shannon, "A Symbolic Analysis of Relay and Switching Circuits", Transactions of the AIEE vol. 57 (1938)
  • abstract of his MIT master's thesis. Computer History Museum, The Silicon Engine, "1947: Invention of the Point-Contact Transistor" (Bardeen and Brattain, December 1947, germanium)
  • John von Neumann, "First Draft of a Report on the EDVAC" (30 June 1945, Moore School, reprinted in IEEE Annals of the History of Computing 15:4, 1993).

Checked: 2026-10-17