Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Counting in binary and hexadecimal

After this lesson: I can store whole numbers in binary and hexadecimal.

45 min lesson6 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

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

WordWhat it meansExample
Bitone binary digit, a 0 or a 1
Bytea group of eight bits
Denaryordinary counting, with the ten digits 0 to 9
Binarycounting with only the digits 0 and 1101010
Place valuewhat a digit is worth because of where it sits
Hexadecimalcounting in sixteens, with 0 to 9 and A to FFF

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 value1286432168421
Bit00101010
Worth003208020

The cells students fill in are left blank in this preview.

Go to the arch folder

Command
cd ~/arch

Shown 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

Command
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

Command
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

NumberDivided by 2Remainder
1361
630
311
101
Read upwards1101

The cells students fill in are left blank in this preview.

The shell reads binary too

Command
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

DenaryBinaryTwo halvesHex
200110010001100 is 12, 1000 is 8C8
255111111111111 is 15, 1111 is 15FF
42001010100010 is 2, 1010 is 102A

The cells students fill in are left blank in this preview.

The shell writes 255 in hex

Command
printf '%x\n' 255

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask Python for two more

Command
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?

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.