Level 0: How computers work · 12 min
Binary: counting with switches
Goal: read and write numbers using only 0 and 1.
You need: Switches.
The problem
One switch gives two choices: 0 or 1. But a computer must work with numbers like 5, 42 or 1000. How?
Use more switches. Each new switch doubles the number of different patterns.
| Switches | Patterns | How many |
|---|---|---|
| 1 | 0, 1 | 2 |
| 2 | 00, 01, 10, 11 | 4 |
| 3 | 000, 001, 010, 011, 100, 101, 110, 111 | 8 |
An easy start: four coins
Imagine you have exactly one coin of each kind: 8, 4, 2 and 1. For each coin you have only two choices: use it (1) or keep it (0).
| Pay | 8 | 4 | 2 | 1 | Binary |
|---|---|---|---|---|---|
| 3 | 0 | 0 | 1 | 1 | 0011 |
| 5 | 0 | 1 | 0 | 1 | 0101 |
| 9 | 1 | 0 | 0 | 1 | 1001 |
| 15 | 1 | 1 | 1 | 1 | 1111 |
With these four coins you can pay every amount from 0 to 15. The row of 0s and 1s is the number in binary. The rest of this lesson shows why it works.
First, look at the numbers you already use
You count with ten digits, 0 to 9. This is called decimal. In the number 352, each position has a value:
| Position value | 100 | 10 | 1 |
|---|---|---|---|
| Digit | 3 | 5 | 2 |
So 352 means 3 hundreds + 5 tens + 2 ones. Each position is worth 10 times the one on its right.
Binary works the same way
Binary has only two digits, 0 and 1. So each position is worth 2 times the one on its right: 1, 2, 4, 8, 16 and so on.
To read a binary number, add the values of the positions that have a 1.
| Position value | 4 | 2 | 1 |
|---|---|---|---|
| Bit | 1 | 0 | 1 |
101 = 4 + 1 = 5. We skip the 2, because its bit is 0.
Try it
Each box is one bit. The small number is what the bit is worth. Click a box to switch it on or off.
Try to make 5, then 10, then 42. Then press "Add 1" many times and watch the right side.
0
Counting from 0 to 7
| Decimal | Binary (4, 2, 1) |
|---|---|
| 0 | 000 |
| 1 | 001 |
| 2 | 010 |
| 3 | 011 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
When you add 1 in decimal and a position is already 9, it goes back to 0 and you carry 1 to the left. Binary does the same, but it happens as soon as a position is 1. So 1 + 1 = 10 in binary. We read it "one zero". It means two.
Writing a number in binary
Start with the biggest position value that fits. Take it away, then repeat with what is left.
Example: write 13 with the positions 8, 4, 2, 1.
| Position | Does it fit? | Bit | Left over |
|---|---|---|---|
| 8 | yes, 8 fits in 13 | 1 | 5 |
| 4 | yes, 4 fits in 5 | 1 | 1 |
| 2 | no, 2 does not fit in 1 | 0 | 1 |
| 1 | yes | 1 | 0 |
So 13 = 1101. Check: 8 + 4 + 1 = 13.
Bytes
A group of 8 bits is called a byte. It has 256 patterns, so it can hold the whole numbers from 0 to 255. Bigger numbers use more bytes.
Letters, colours and sounds are stored as numbers too. People agree on rules that say which number means what. You will see these rules later in the course.
Check yourself
Practice
- Read 1011 as a decimal number.
- Write 22 in binary.
Show answer
1. 1011 = 8 + 2 + 1 = 11 2. 22 = 16 + 4 + 2 = 10110
Recap
- Binary uses two digits, so each position is worth twice the one on its right.
- To read it, add the values of the positions that have a 1.
- Each extra bit doubles the number of patterns.
- 8 bits make a byte, which can hold 0 to 255.