The level that broke a game
In the arcade game Pac-Man, level 256 is broken. Half the screen fills with jumbled letters and nobody can finish it.
The game kept the level number in eight tiny switches. Eight switches can only count from 0 to 255. By the end of this lesson you will see why.
By the end of this lesson
Note
I can store whole numbers in binary and hexadecimal.
Key words
| Word | What it means | Example |
|---|---|---|
| Bit | one binary digit, a 0 or a 1 | |
| Byte | a group of eight bits | |
| Denary | ordinary counting, with the ten digits 0 to 9 | |
| Binary | counting with only the digits 0 and 1 | 101010 |
| Place value | what a digit is worth because of where it sits | |
| Hexadecimal | counting in sixteens, with 0 to 9 and A to F | FF |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top. The lab is a whole Ubuntu machine of your own, so it takes a minute to start. Each Run button types its command into the machine's terminal. If a Run says the machine is still starting, wait and press it again.
Binary uses only 0 and 1
Inside a computer every number is held by switches that are off or on. We write off as 0 and on as 1.
In denary each place is worth ten times the place on its right. In binary each place is worth twice as much. A byte has eight places, worth 128, 64, 32, 16, 8, 4, 2 and 1. Add up the places that hold a 1.
Worked example: 42 in one byte
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
| Worth | 0 | 0 | 32 | 0 | 8 | 0 | 2 | 0 |
The cells students fill in are left blank in this preview.
Go to the arch folder
cd ~/archShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask Python for 42 in binary
python3 -c "print(bin(42))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Turn the bits back into denary
python3 -c "print(int('101010', 2))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Halving turns denary into binary
To write a number in binary, divide it by 2 and write down the remainder. Divide the answer by 2 again. Keep going until you reach 0.
The remainders are the bits. Read them from the last one up to the first.
Worked example: 13 by halving
| Number | Divided by 2 | Remainder |
|---|---|---|
| 13 | 6 | 1 |
| 6 | 3 | 0 |
| 3 | 1 | 1 |
| 1 | 0 | 1 |
| Read upwards | 1101 |
The cells students fill in are left blank in this preview.
The shell reads binary too
echo $((2#1101))Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people read binary from the left, as if the first bit were worth 1. The rightmost bit is worth 1. Each place to its left is worth twice as much. So 1000 is eight, and 0001 is one.
Hexadecimal is shorthand for binary
Long rows of bits are hard to read. Hexadecimal counts in sixteens. It uses 0 to 9, then A to F for ten to fifteen.
One hex digit stands for exactly four bits. So one byte is always two hex digits. Split the byte into two halves of four bits and write each half as one digit.
Worked example: bytes as hex
| Denary | Binary | Two halves | Hex |
|---|---|---|---|
| 200 | 11001000 | 1100 is 12, 1000 is 8 | C8 |
| 255 | 11111111 | 1111 is 15, 1111 is 15 | FF |
| 42 | 00101010 | 0010 is 2, 1010 is 10 | 2A |
The cells students fill in are left blank in this preview.
The shell writes 255 in hex
printf '%x\n' 255Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask Python for two more
python3 -c "print(hex(200), hex(4096))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further: colours in hex
Web pages often write a colour as six hex digits, such as #FF8800. The first two digits are red, the next two green and the last two blue.
Work out each pair in denary by hand. Then check your answers with to_hex from the assignment. What colour do you think it is?