Binary and languages: make instructions readable
Welcome back! In the last lesson, a program added up the committee's payments. But how does a computer hold a number like 2,500, or an instruction like "add"?
In this lesson, you'll learn:
- how a computer stores everything using only two values;
- why the same pattern can mean different things;
- how people went from writing 0s and 1s to writing languages like Python.
Why it matters: a computer never knows what you meant. It only follows the way it was told to read things. Once you see that, you'll understand how a program can run perfectly and still give the wrong answer.
Everything is stored as 0s and 1s
A computer stores information using tiny electronic parts. Each part can be in one of two states. You can picture each one as a switch that is either off or on. We write off as 0 and on as 1.
One 0-or-1 place is called a bit, short for binary digit. Binary means using just two values.
The same pattern can mean different things
Look at this pattern of four bits: 1010.
On its own, it means nothing. We have to agree what it stands for:
- We could agree that each place is one committee member at a meeting, where 1 means present and 0 means absent. Then
1010means the first and third members came. - We could agree to read it as a number. Read that way, it means 10.
A pattern only means something once we agree how to read it. Numbers, letters, pictures and instructions are all stored as bit patterns, each with its own agreed way of reading.
This matters for the committee too. Say the organiser uses 1 to mean "paid", but the program reads 1 as "came to the meeting". The program will count the wrong thing, and nothing will look broken. Someone has to decide the reading first, and write it down.
From 0s and 1s to languages people can read
We'll follow one small job through three levels: add 6 and 3. We use small numbers so the bit patterns stay short.
Machine code: the bottom layer every program must reach
The processor carries out instructions: small steps such as "add". It understands only machine code, which means instructions written as bit patterns.
For this example, imagine a very simple processor. Each instruction has three pieces of four bits: what to do, then the two numbers to use. This one means "add 6 and 3":
| Piece | What to do | First number | Second number |
|---|---|---|---|
| Bits | 0001 | 0110 | 0011 |
| Meaning | add | 6 | 3 |
The Meaning row tells you what each piece stands for, so you don't need to work out the numbers yourself.
The whole instruction is 0001 0110 0011. Another processor might use 1100 for "add". Different kinds of processor use different machine code, so a program written for one may not run on another.
Assembly: short names, so people stop writing rows of bits
Writing long rows of 0s and 1s by hand was slow, and mistakes were easy. So people invented assembly language, which uses short, readable names for instructions. On our simple processor, the same job is written like this:
ADD 6, 3
An assembler is a program that turns this back into bits. ADD becomes 0001, and each number becomes its four bits, so you get 0001 0110 0011.
Higher-level languages: say what you want, not each step
Later came higher-level languages. They let people say what they want done, without choosing each processor instruction. Fortran, one of the first, came in the 1950s. It let scientists write calculations that looked like the formulas they already knew.
Python is a modern higher-level language, and it's the one you'll read in this book. The same job looks like this:
total = 6 + 3
One line says what you want, and gives the answer the name total. You don't need to know any bit patterns to read it.
One job, three levels: why each step up is easier to read
| Level | What it looks like | The same job |
|---|---|---|
| 1. Machine code | Bit patterns | 0001 0110 0011 |
| 2. Assembly | Short names | ADD 6, 3 |
| 3. Higher-level | One readable line | total = 6 + 3 |
Writing instructions in any of these languages is called programming.
Who translates your program
The program text that a person writes is called source code. The processor can't read it directly, so other software has to translate it or carry it out:
- A compiler translates the whole program into another form before it runs.
- An interpreter reads the program and carries out its instructions as it goes.
Many real tools mix the two. To run Python, you use a program that understands Python, and that program is usually called Python too.
So whenever you run a program, ask two separate questions:
- Did it run? Did the tools accept the program, or did they stop with an error?
- Did it do the right thing? Did it do what the person actually needed?
A program can pass the first question and fail the second. In the last lesson, the 3,000 total ran perfectly and still answered the wrong question.
Check what you can now explain
Without looking back:
- Why can
1010mean "the first and third members came" or the number 10? - What does a compiler do? And what can't it know about what the organiser needs?
- A member's record shows
0101. Before anyone writes a program that uses it, what would you ask the organiser, and why?
You don't need to remember the pattern. What matters is the idea: meaning comes from an agreed way of reading.
Next, we'll find where a program actually lives and runs: Files, editors and terminals.