1Why does a computer count with only 0 and 1?
A switch is either off or on. A chip is billions of tiny switches. So how do you get 23 out of switches?
The 40-second version: switch, transistor, gate, adder, chip, phone. This page goes through it one step at a time.
Play
Tap the lamps. Can you make 23, the reel's orange number? Then 19, then 42.
Work one by hand: 23
Each lamp has a value: 128, 64, 32, 16, 8, 4, 2 and 1. Start from the biggest and ask "does it fit?"
- 128, 64 and 32 are bigger than 23. Those lamps stay off.
- 16 fits. Switch it on. 23 − 16 = 7 left.
- 8 is bigger than 7. Off.
- 4 fits: 7 − 4 = 3 left. 2 fits: 3 − 2 = 1 left. 1 fits: 1 − 1 = 0. Done.
Lamps on: 16, 4, 2 and 1. Write on as 1 and off as 0, from left to right: 00010111. That's how the reel wrote 23.
- 2316 + 4 + 2 + 1 = 00010111
- 1916 + 2 + 1 = 00010011
- 4232 + 8 + 2 = 00101010
See it
Each lamp is worth twice the lamp to its right. To read a row, add up the lamps that are on.
You already do this with tens. In 42, the 4 means 4 tens and the 2 means 2 ones. Each place is worth 10 times the place to its right: 1, 10, 100, 1000. With only 0 and 1 to write with, each place is worth 2 times instead.
Imagine one coin each of ₹1, ₹2, ₹4, ₹8, ₹16, ₹32, ₹64 and ₹128. You can pay any amount from ₹0 to ₹255 exactly. Each coin is either in your hand or not, just like a lamp.
Where it stops: with real coins you could carry two ₹2 coins. Here each place gets one lamp, used once or not at all.
Name it
One switch's value, 0 or 1, is a bit. Counting with only 0 and 1 is called binary.
In binary, 10 means "one 2 and no 1s". That's two. People misread it all the time, so always ask which kind of counting a number is in.
Formula last: why doubling?
Why are the places worth 1, 2, 4, 8 and not 1, 2, 3, 4? Doubling is the only way every pattern gives a different number, with no gaps.
- Three lamps worth 1, 2 and 4 can make every number from 0 to 7, because 1 + 2 + 4 = 7.
- The next lamp should start where they stop. So it must be worth 7 + 1 = 8.
- Each new lamp is worth all the lamps before it, plus 1. That always comes out as double the last one.
Each b is one lamp, 0 or 1, counted from the right. For 23, b4, b2, b1 and b0 are 1, so the value is 16 + 4 + 2 + 1. All 8 lamps on make 255. So 8 bits hold 256 different numbers: 0 to 255.
A surprising number: phone processors work with 64 bits at a time, not 8. The biggest 64-bit number is 18,446,744,073,709,551,615. That's what doubling does: each extra bit doubles the range.
Try it: 42 on paper
Turn a number into bits with no calculator, the same way as 23 above.
- Write the places: 128 64 32 16 8 4 2 1.
- Go left to right. If a place fits in what's left, write 1 and take it away: 42 − 32 = 10, 10 − 8 = 2, 2 − 2 = 0. Write 0 under the rest.
- You should get 00101010. Check it in Python:
format(42, '08b').
Then do your age and your house number.
So: a row of on/off switches can hold any whole number, because each place is worth double the one to its right.
Go deeper (optional)
- Binary numberWikipedia contributors · CC BY-SA 4.0
2How can electricity flip a switch?
Your finger flips a light switch. A chip has no fingers. Something electric has to do the flipping.
Play
See it
The teal block has three wires. Power comes in on one side. It goes out the other side to the lamp.
The third wire, A, is the control. Put a voltage on A and the block lets current through. Take the voltage away and it blocks the current. Nothing moves inside.
Why this matters: the control is electric. So one switch's output can flip the next switch, with no person in the loop. That's what lets switches do maths on their own.
It works like a water tap that is opened by a second, small flow instead of a hand. Small flow on the control: big flow through.
Where it stops: a tap can be half open, and so can a real transistor. Chips only use the two ends, fully off (0) and fully on (1).
Name it
A switch flipped by electricity is a transistor. Now switch the Play to "Two in a row". Current must get through both, so the lamp lights only when A is 1 and B is 1. A small circuit that turns input bits into an output bit is a logic gate. This one is the AND gate.
Formula last
Write a closed switch as 1 and an open one as 0. One open switch in the row blocks everything. Multiplying works the same way: any 0 makes the answer 0.
Check all four cases: 0 × 0 = 0, 0 × 1 = 0, 1 × 0 = 0, 1 × 1 = 1.
What a real chip does differently
The picture above is the simple version. In a real chip, transistors work in pairs. One kind connects the output to power. The other kind connects it to ground. So the output is always pushed firmly to 1 or to 0, and almost no current flows while nothing changes. That's why your phone doesn't melt.
The two-in-a-row path you just played with is the bottom half of a real gate.
Build: an AND gate you press
Two push buttons in a row, an LED and a resistor. Your fingers are the transistors.
- Battery (+) → button A → button B → resistor → LED long leg. LED short leg → battery (−).
- Example values: 2 x AA = 3 V. A red LED uses about 2 V. So a 100 Ω resistor gives (3 − 2) ÷ 100 = 10 mA. These are illustrative: check your LED's datasheet.
- Press one button, then both. Then wire the buttons side by side instead. Now either button lights it. That's an OR gate.
Never leave out the resistor. Without it the LED takes too much current and burns out.
So: a transistor is a switch that electricity flips, and two in a row make an AND gate.
Go deeper (optional)
- TransistorWikipedia contributors · CC BY-SA 4.0
- CMOS (the transistor pairs)Wikipedia contributors · CC BY-SA 4.0
3Which gate means "one or the other, but not both"?
AND needs both inputs on. To add, you'll need a gate that answers a different question.
Play
| A | B | A AND B |
|---|
Flip A and B and watch which row of the table lights up. Then pick another gate. Which one lights for exactly one input?
See it
Two inputs give only four cases. So every gate is just a table with four rows.
- ANDlights on the last row only: both on.
- ORlights on any row with a 1.
- XORlights on the two middle rows: the inputs are different.
A stair light with a switch at the bottom and one at the top is wired this way. Flip either switch and the light changes. That only works if the light depends on whether the two switches match.
Where it stops: whether "match" means on or off depends on how the electrician wired it. A gate is always the same.
Name it
"Exactly one is on" is the XOR gate, short for exclusive or. It's built from transistors too, just more of them than AND.
Formula last
You can write each table as ordinary sums with 0 and 1. Start from A + B. It counts how many inputs are on: 0, 1 or 2.
- OR should be 1 when the count is 1 or 2. The count is 2 only on the last row, where A×B = 1. So take that off once:OR = A + B − A×B
- XOR should be 0 on that last row. So take it off twice:XOR = A + B − 2×A×B
- Move the last part to the other side. Remember from lesson 2 that A×B is AND:
Check it on the last row: 1 + 1 = 0 + 2 × 1. Yes, both sides are 2. On the left is addition. On the right are two gates. That line is the whole trick of the next lesson.
So: XOR says "the inputs are different", and XOR plus two ANDs equals A + B.
Go deeper (optional)
- Logic gateWikipedia contributors · CC BY-SA 4.0
- NAND logic: every gate from one kind of gateWikipedia contributors · CC BY-SA 4.0
4How do gates add 1 + 1?
In binary there's no digit 2. So what does 1 + 1 come out as?
Play
Work one by hand: 1 + 1
- 1 + 1 = 2. But a binary place can only hold 0 or 1.
- 2 is one 2 and no 1s. So write 0 in this place and carry 1 to the next place, which is worth 2.
- The answer is written 10. In binary, that's two. This is the reel's 1 + 1 = 10.
Now do it with gates. Set A = 1 and B = 1 in the Play. XOR(1, 1) = 0: the inputs aren't different, so the sum lamp stays off. AND(1, 1) = 1: the carry lamp lights. Same answer: carry 1, sum 0.
In tens, 5 + 7 = 12. You write the 2 and carry the 1. Binary does the same thing.
Where it stops: in tens you carry once a column reaches 10. In binary you carry once it reaches 2, so you carry much more often.
Name it
An XOR and an AND side by side, fed the same two bits, make a one-column adder. It's called a half adder. It has two outputs: the sum S, the digit you write, and the carry C, the digit you pass left.
Formula last
It's lesson 3's line, with the gates given their adder names:
Why is the carry worth 2? It moves one place left, and each place is worth double (lesson 1). For our example: 2 × 1 + 0 = 2.
Why "half"? It only takes two inputs. Every column after the first also gets a carry from its right, so it has three bits to add. That's lesson 5.
So: a half adder is an XOR for the digit you write and an AND for the carry.
Go deeper (optional)
- Adder (electronics)Wikipedia contributors · CC BY-SA 4.0
5How do eight of them add 23 + 19?
The reel's chip added 00010111 + 00010011 and got 00101010. Walk through it one column at a time.
Play
Work it by hand: 23 + 19
Write the two numbers in bits, one above the other, like school addition. Start at the right. Each column adds its two bits plus any carry, writes one bit and carries the rest left.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|---|---|---|
| carry in | 1 | 1 | 1 | 1 | ||||
| 23 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 |
| 19 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |
| 42 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
- Ones column: 1 + 1 = 2. Write 0, carry 1.
- Twos column: 1 + 1 + carry 1 = 3. Three is one 2 and one 1, so write 1, carry 1.
- Fours column: 1 + 0 + carry 1 = 2. Write 0, carry 1.
- Eights column: 0 + 0 + carry 1 = 1. Write 1, no carry.
- Sixteens column: 1 + 1 = 2. Write 0, carry 1. Thirty-twos: 0 + 0 + carry 1 = 1. Write 1. The last two columns are all 0.
Answer: 00101010 = 32 + 8 + 2 = 42. The carries out, from the right, were 1, 1, 1, 0, 1, 0, 0, 0.
See it
Each teal box in the Play does one column of that table. It takes its bit of A, its bit of B and the carry from its right. It writes one sum bit and passes one carry left.
A column can never get more than 1 + 1 + 1 = 3. Three is 11 in binary: write 1, carry 1. So one carry wire between boxes is always enough.
The carry travels right to left, box by box, like dominoes knocking each other over. In 23 + 19 the first three columns each pass a carry on, one after another. That's the ripple in the reel.
Where it stops: a falling domino always knocks the next one. A carry stops as soon as a column has nothing to pass on, like the eights column above.
Name it
A box that adds three bits is a full adder. It's two half adders and an OR gate. The first half adder adds A and B. The second adds the carry in. The OR passes a carry on if either of them made one.
The rightmost box never gets a carry in, so a half adder is enough there. That's how the reel built it: bit 0 is a half adder, bits 1-7 are full adders.
Formula last: why the row adds up
Give each part of column i a letter. Its bits are ai and bi. Its carry in is ci. It writes the sum bit si and passes the carry ci+1. Lesson 4's rule, with three inputs, says for every box:
In the twos column above: 1 + 1 + 1 = 2 × 1 + 1. Both sides are 3.
- Multiply each column's line by that column's value (1, 2, 4 … 128). Then add all eight lines together.
- On the left, the a parts add up to A and the b parts to B, by lesson 1's formula. On the right, the s parts add up to the sum S.
- Each carry appears twice. It's on the right of one column, times 2. It's on the left of the next column, which is worth double. Same value on both sides, so they cancel. Only the carry out of the last column is left over.
For 23 + 19 the last carry is 0, so the row holds the whole answer: 42. If it's 1, the answer needs a ninth bit. Try 200 + 100 in the adder at the top of the page. The true answer is 300. The row keeps 44 and carries 1 out, and 300 − 256 = 44. calcs.py checks this formula on all 65,536 pairs of 8-bit numbers.
They don't. Each box must wait for the carry from its right. In the worst case a carry has to pass through all 8 boxes before the answer is ready. Fast chips use extra gates that work out the carries ahead of time, called carry-lookahead.
From one adder to your phone
The adder is one part of a processor, or CPU: the part of a chip that follows instructions. Around the adder sit memory, which holds the numbers, and control, which decides what to do next. A phone chip has several of these, and the whole chip is billions of transistors. Every one is the switch from lesson 2.
Where it's used: every time your phone adds anything. A score in a game, the total in a shopping cart, the next pixel along a line on your screen, the time on your clock ticking forward by one.
How many transistors is this adder?
One way to count: build everything from a single kind of gate, a "flipped AND" that's 1 unless both inputs are 1. It takes 4 transistors. A half adder needs 5 of these gates and a full adder needs 9. So this 8-bit adder needs 5 + 7 × 9 = 68 gates, which is 272 transistors.
Real chips use smarter circuits, but the scale is right. A chip with billions of transistors has room for millions of adders like this one.
Try it: an adder in Python
Build the adder out of gates in code, then make it add 23 + 19.
def half(a, b):
return a ^ b, a & b # XOR, AND
def full(a, b, c):
s1, c1 = half(a, b)
s, c2 = half(s1, c)
return s, c1 | c2 # OR
def add8(x, y):
carry, total = 0, 0
for i in range(8):
s, carry = full(x >> i & 1, y >> i & 1, carry)
total += s << i
return total, carry
print(add8(23, 19))
print(add8(200, 100))
- Expected output:
(42, 0)then(44, 1). The second one is too big for 8 bits. - Then on paper: add your age to a friend's age in bits, with the carries on top.
Bit 0 uses full with a carry in of 0, which gives the same answer as a half adder.
So: eight full adders in a row add two 8-bit numbers, one column at a time, passing carries left.
Go deeper (optional)
- Nand to Tetris: build a computer from one kind of gateNoam Nisan and Shimon Schocken · free online course
- 6.004 Computation StructuresMIT OpenCourseWare · CC BY-NC-SA
- Carry-lookahead adderWikipedia contributors · CC BY-SA 4.0
Recap
- A row of switches is a number. Each place is worth double: 23 is 00010111.
- A transistor is a switch flipped by electricity. Two in a row make an AND gate.
- XOR means "different". A + B = XOR + 2 × AND, so an XOR and an AND add one column: 1 + 1 = 10.
- Full adders in a row pass carries left: 00010111 + 00010011 = 00101010, which is 23 + 19 = 42.
- Billions of transistors doing this is the chip in your phone.
Build your first robot
HalfSmart, a 3D-printed 5-axis robot arm, is coming to Jig Pro as a full step-by-step build. Join the waitlist and we'll tell you when it opens.
Join the waitlist