Skip to main content

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.

SwitchesPatternsHow many
10, 12
200, 01, 10, 114
3000, 001, 010, 011, 100, 101, 110, 1118

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).

Pay8421Binary
300110011
501010101
910011001
1511111111

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 value100101
Digit352

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 value421
Bit101

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

DecimalBinary (4, 2, 1)
0000
1001
2010
3011
4100
5101
6110
7111

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.

PositionDoes it fit?BitLeft over
8yes, 8 fits in 1315
4yes, 4 fits in 511
2no, 2 does not fit in 101
1yes10

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

1. What is the binary number 110 in decimal?

2. What is 1 + 1 written in binary?

3. How many different patterns can 4 bits make?

Practice

  1. Read 1011 as a decimal number.
  2. 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.