A computer is billions of tiny switches
Think about a light switch. Flip it up and the light is on. Flip it down and the light is off. A computer is built from billions of switches like that, but they are far too small to see and they flip by themselves, billions of times each second.
The tiny switch is called a transistor. It has no handle. Instead, a small electrical signal flips it. That means one switch can control another switch. That one trick is how a pile of switches can start to make decisions.
Here are the words you will use again and again in this tutorial.
- Transistor
- A tiny switch with no handle. An electrical signal turns it on or off.
- Signal
- Electricity that carries a message. It is either high (on) or low (off).
- Bit
- One signal that is either 1 (on) or 0 (off). It is short for “binary digit.”
- Logic gate
- A small group of transistors that turns input bits into one output bit using a fixed rule.
- Truth table
- A list of every possible input and the output the gate gives for each one.
- Boolean
- A value that is only true or false. Computers store it as 1 or 0.
Switch lab: how two switches make a rule
Try the three circuits below. Click a switch, or focus it and press Enter. Watch where the glowing trace stops.
One transistor
A control signal closes the switch inside.
Switches in a row
Power must pass through both.
Switches side by side
Power can take either path.
Switches in a row act like AND. The lamp needs switch A and switch B. Switches side by side act like OR. The lamp needs switch A or switch B. Engineers build logic gates from transistors using exactly these two ideas.
Meet the logic gates
A logic gate has one or two inputs and one output. Each is a bit, 1 or 0. The gate follows one rule, the same way every time. It is like a bouncer at a door who only ever checks one thing.
AND: the strict bouncer
“You need a ticket and an ID.” The output is 1 only if both inputs are 1.
OR: the hallway light
A switch at each end of the hall. Flip either one and the light comes on. The output is 1 if at least one input is 1.
NOT: the opposite machine
It has one input. It flips it. Put in 1 and get 0. Put in 0 and get 1.
Three more gates are just as common. NAND means “NOT AND.” It does AND and then flips the answer. NOR means “NOT OR.” XOR means “exclusive or.” It is 1 when the inputs are different, like choosing one dessert or the other, but not both.
Gate playground
Pick a gate. Click the input circles on the board to flip them between 0 and 1. The table fills in every possible case, and your current case is highlighted.
Spot the patterns. AND has just one 1 in its output column. OR has just one 0. XOR is 1 exactly when the two inputs differ. NAND, NOR, and NOT are the flipped versions of AND, OR, and the plain wire.
Wiring gates together
One gate makes one tiny decision. The real magic starts when the output of one gate becomes the input of the next.
Say you are building a home alarm. It should sound only when the door is open and the alarm is armed. That is one AND gate. Now say you also want a way to switch it off for pets. Add a NOT gate and a second AND gate, and you get a new rule. Every circuit in a computer is built this way: small gates, wired in layers.
NAND is a good example. You can build it from two simpler gates: send the inputs through AND, then send that result through NOT. In words, that is “not both.” Chip makers love NAND because every other gate can be built out of NAND gates alone. That is why it is called the universal gate.
Read circuits left to right. Signals flow from the inputs on the left, through each layer of gates, to the output on the right. In the puzzles next, a wire can only go from an earlier column to a later one.
Circuit puzzles: wire it up
Each puzzle gives you a goal table. Choose the gate types, connect the wires, and the board checks every possible input for you.
- Pick a gate type in each gate box (the drop-down menu).
- Click or tap a filled output circle, then click an input circle to connect them. You can also drag a wire from one circle to the other.
- Use the 0/1 buttons on the left to test. Use the small X button on a box to unplug its wires.
Tip: swipe the board sideways if it does not all fit.
Adding with gates: the half adder
Computers count in binary, using only 0 and 1. In binary, 1 + 1 does not make 2. It makes 10, which you read as “one, zero.” You write 0 and carry the 1, just like carrying in school math.
Two gates can do that for one column of bits. An XOR gate makes the sum bit (1 when the inputs differ). An AND gate makes the carry bit (1 only when both inputs are 1). Together they are called a half adder.
Why “half”? It cannot accept a carry that arrives from the column to its right. Join two half adders and one OR gate and you get a full adder, which can. Chain full adders in a row and you can add numbers of any size.
Four-bit adder
Toggle the bits of two numbers. Each column is worth double the one to its right: 1, 2, 4, then 8. Watch the carries ripple from right to left, just like on paper.
sum = A XOR B XOR carry-in
carry-out = (A AND B) OR (carry-in AND (A XOR B))
Try 15 + 1. The answer is 16, which needs a fifth bit. A real chip with only four sum bits would lose that last carry. This is called overflow, and it is why bigger chips use 32 or 64 bits.
From gates to a whole computer
You just built the same ideas that sit inside every processor. Here is how the layers stack up.
- Transistors are tiny switches. A modern chip can hold billions of them.
- Logic gates are a few transistors wired to follow a rule like AND or NOT.
- Adders and other circuits are gates wired in layers to do math and make choices.
- The processor (CPU) combines many circuits with memory so it can follow instructions.
- Your programs are lists of instructions. Every
ifand every+ends up as signals flowing through gates like these.
Want to see a whole tiny computer built from these parts? Read What Is a Microcontroller?. To see how bits stand for numbers, try the Binary Explorer.
Gates in your code
You already know these gates if you have written an if statement. The Boolean operators in code are the same ideas. True acts like 1 and False acts like 0.
# columns: a, b, AND, OR, NOT a, XOR for a in [False, True]: for b in [False, True]: print(a, b, a and b, a or b, not a, a != b)
False False False False True False False True False True True True True False False True False True True True True True False False
Python even has bitwise operators that work on the bits of whole numbers. & is AND, | is OR, ^ is XOR, and ~ is NOT. That means you can write the half adder from this page in two lines.
def half_adder(a, b): total = a ^ b # XOR gives the sum bit carry = a & b # AND gives the carry bit return total, carry print(half_adder(1, 1)) # 1 + 1 = 10 in binary print(half_adder(1, 0))
(0, 1) (1, 0)
The first line printed (0, 1): sum 0, carry 1. That matches the half adder lab. To practice the code side, see Booleans and Logical Operators in Python.
Quick answers
What is a logic gate in simple words?
A logic gate is a tiny circuit that takes one or two inputs, each either on (1) or off (0), and follows a fixed rule to decide whether its output is on or off. An AND gate, for example, turns on only when both inputs are on.
What are the basic logic gates?
The three basic gates are AND, OR, and NOT. AND outputs 1 only if both inputs are 1. OR outputs 1 if at least one input is 1. NOT flips its single input. NAND, NOR, and XOR are built from those ideas and are used just as often.
How do transistors make logic gates?
A transistor is a switch that a small electrical signal can turn on or off. Wire two transistors one after the other and current flows only when both are on, which acts like AND. Wire them side by side and current flows when either is on, which acts like OR.
Why is NAND called a universal gate?
Every other gate, and therefore every digital circuit, can be built using only NAND gates. Wiring both inputs of a NAND gate together makes a NOT gate, and a NAND followed by a NOT makes an AND gate. That is why chip makers like it.
How does a computer add numbers with logic gates?
A half adder uses an XOR gate to make the sum bit and an AND gate to make the carry bit. Chaining adders that also accept a carry-in lets the computer add numbers with many bits, passing the carry from one column to the next like adding on paper.
How are logic gates related to and, or, and not in code?
They are the same ideas. The Boolean operators and, or, and not in Python (and &&, ||, and ! in JavaScript) work exactly like AND, OR, and NOT gates, with True and False standing in for 1 and 0. Your code is ultimately run by billions of real gates.
Quick quiz
1. An alarm should sound only when the door is open AND the alarm is armed. Which gate fits?
2. What does an XOR gate output when both inputs are 1?
3. A half adder adds 1 + 1. What are the sum and carry bits?
Keep learning about how computers work
You now know how switches become gates, how gates combine, and how a few gates can add. Next, explore how bits stand for numbers, meet the tiny computers that use gates, or learn the electricity behind it all.