Logic Gates and Boolean Logic
Work with true and false values, build truth tables for AND, OR, NOT and XOR, and see how simple gates combine into a circuit that adds numbers.
Welcome back!
Step 1 of 14Learn
Logic with only two values
In the 1850s, the English mathematician George Boole had an unusual idea: treat logical statements like algebra, with only two values, true and false. Almost a century later, Claude Shannon showed that Boole's algebra could describe electrical switching circuits. That insight is the foundation of every digital computer.
In Boolean logic, true is written as 1 and false as 0. Three basic operations do most of the work:
- NOT flips a value: NOT 1 = 0, NOT 0 = 1.
- AND is 1 only if both inputs are 1. "I'll go if it's sunny AND I've finished homework."
- OR is 1 if at least one input is 1. "I'll be happy if I get pizza OR tacos," including if you get both.
A truth table lists every input combination and its result. For two inputs there are four rows:
| A | B | A AND B | A OR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
You use Boolean logic every time you filter a search: "shoes AND waterproof AND NOT boots."
Step 2 of 14Practice · Fill in the blanks
Fill in the blanks
1 AND 0 = . 1 OR 0 = . NOT 1 = .
Step 3 of 14Learn
Gates: logic built from switches
A logic gate is a tiny circuit, made from a few transistors, that performs one Boolean operation on electrical signals. High voltage means 1, low voltage means 0.
Besides AND, OR and NOT, two more gates are especially useful:
- XOR (exclusive or) outputs 1 when the inputs are different: 0 XOR 1 = 1, but 1 XOR 1 = 0. Think "one or the other, but not both," like a hallway light with switches at both ends.
- NAND (NOT AND) outputs 0 only when both inputs are 1. Remarkably, every other gate can be built from NAND gates alone, which is one reason chips can be manufactured from huge numbers of identical building blocks.
| A | B | A XOR B | A NAND B |
|---|---|---|---|
| 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
Gates can be chained so that one gate's output feeds another's input. With enough gates you can build circuits that compare numbers, add them, store bits and decide which instruction to run next. A modern processor contains billions of transistors forming these gates, all switching billions of times per second.
Step 4 of 14Practice · Match the pairs
Match each gate with the rule for when it outputs 1.
Tap an answer, then tap the place where it belongs (or drag it there).
- AND
- OR
- XOR
- NAND
- NOT
Step 5 of 14Compare
Truth tables side by side
| Aspect | AND | OR | XOR | NAND |
|---|---|---|---|---|
| A=0, B=0 | 0 | 0 | 0 | 1 |
| A=0, B=1 | 0 | 1 | 1 | 1 |
| A=1, B=0 | 0 | 1 | 1 | 1 |
| A=1, B=1 | 1 | 1 | 0 | 0 |
Step 6 of 14Practice · Code challenge
Write a function xor(a, b) that takes two Booleans and returns true only when exactly one of them is true. Use && (AND), || (OR) and ! (NOT), or compare the values directly.
xor(true, false)xor(false, true)xor(true, true)xor(false, false)
Step 7 of 14Learn
From gates to arithmetic: the half adder
How does a computer add? Start with adding two single bits:
| A | B | Sum | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
The last row is 1 + 1 = 2, which in binary is 10: write 0, carry 1, just like 5 + 5 = 10 in decimal.
Now look at the columns. The Sum column is exactly XOR. The Carry column is exactly AND. So two gates, one XOR and one AND, make a half adder, a circuit that adds two bits.
To add multi-bit numbers, you also need to add the carry coming from the column to the right. A full adder handles three inputs (A, B and carry-in) using two half adders and an OR gate. Chain 64 full adders together, each passing its carry to the next, and you can add two 64-bit numbers.
This is the magic of abstraction again. Transistors make gates, gates make adders, adders become part of the ALU, and the ALU runs every calculation in every program you've ever used. Nothing in the stack is mysterious on its own; the power comes from combining simple parts.
Step 8 of 14Diagram
Inside a half adder
Chain a carry into the next column and you get a full adder; chain 64 of those and you can add 64-bit numbers.
Tap a shape with to see more.
- Half adder
- Input A
One bit, 0 or 1.
- Input B
The other bit.
- XOR gate → Sum
1 when exactly one input is 1.
- AND gate → Carry
1 only when both inputs are 1, that is 1 + 1 = 10 in binary.
Step 9 of 14Practice · Code challenge
A half adder adds two bits. Complete halfAdder(a, b) so it returns an array [sum, carry], where each value is 0 or 1. Remember: sum is XOR, carry is AND.
halfAdder(0, 0)halfAdder(0, 1)halfAdder(1, 0)halfAdder(1, 1)
Step 10 of 14Note
Step 11 of 14Note
Step 12 of 14Practice · True or false
The expression (1 OR 0) AND (NOT 1) evaluates to 1.
Step 13 of 14Try the calculator
Gate playground
Set each input to 0 or 1 and watch every gate respond.
- A AND B
- 0
- A OR B
- 1
- A XOR B
- 1
- A NAND B
- 1
- NOT A
- 0
Step 14 of 14Practice · Choose one
A security door opens only if a valid card is scanned AND the correct PIN is entered, OR if the fire alarm is active. Which Boolean expression describes when it opens?
Lesson complete!