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

Free lesson

How text, pictures and sound become bits

After this lesson: I can say how text, a picture and a sound become bits.

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

Why a song is bigger than a message

A text message is a few hundred bytes. A photo from your phone is a few million. Three minutes of music, stored the way a CD stores it, is about 32 million bytes.

All three are only bits. This lesson shows how each one becomes bits, and how to work out the size.

By the end of this lesson

Note

I can say how text, a picture and a sound become bits.

Key words

WordWhat it meansExample
Character codethe number that stands for one letter or sign65 for A
Unicodeone list of codes for the letters of every language
Pixelone dot of colour in a picture
Colour depthhow many bits each pixel uses
Sample ratehow many times a second a sound is measured
Bit depthhow many bits each sound measurement uses

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 and wait for the machine to say it is running. Each Run button types its command into the machine's terminal, one after another.

Every character has a number

A computer stores a letter as its character code. In the oldest common list, A is 65, B is 66 and a is 97. A space is 32. The end of a line is a character too, number 10.

Unicode gives a code to every letter in every language, and to emoji. Its first 128 codes are the old list, and each of those takes one byte.

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 three codes

Command
python3 -c "print(ord('A'), ord('a'), chr(66))"

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

Save the word Hi in a file

Command
echo Hi > hi.txt

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

Show each byte as a number

Command
od -t u1 hi.txt

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

Count the bytes in the file

Command
wc -c hi.txt

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

A picture is a grid of pixels

A picture is stored as a grid of pixels. The grid's width times its height is the number of pixels. That is the picture's resolution.

Each pixel is a number for its colour. With a colour depth of 1 bit, a pixel is black or white. With 8 bits there are 256 colours. Phones use 24 bits, about 16.7 million colours.

Worked example: the size of a picture

PicturePixelsBits per pixelBitsBytes
3 by 2, black and white616less than 1
100 by 50, 256 colours5,000840,0005,000
800 by 600, full colour480,0002411,520,0001,440,000

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

Write a picture of six pixels

Command
printf 'P1\n3 2\n1 0 1\n0 1 0\n' > tiny.pbm

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

Ask what kind of file it is

Command
file tiny.pbm

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

Read the picture as text

Command
cat tiny.pbm

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

A sound is measured many times a second

A microphone turns sound into a wave. The computer measures the wave many times a second. Each measurement is a sample.

The sample rate is how many samples a second. The bit depth is how many bits each sample uses. Size in bits is sample rate × bit depth × channels × seconds. Stereo has two channels.

Worked example: one minute of music

StepSumAnswer
Samples a second44,10044,100
Bits a second, 16 bits, stereo44,100 × 16 × 21,411,200
Bits in 60 seconds1,411,200 × 6084,672,000
Bytes84,672,000 ÷ 810,584,000

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

Let Python do the sum

Command
python3 -c "print(44100 * 16 * 2 * 60 / 8)"

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 stop when the sum is done and give the answer in bytes. The sum gives bits. Divide by 8 to get bytes, then by 1,000 for kilobytes or by 1,000,000 for megabytes.

Going further: more than the pixels

tiny.pbm holds six pixels, which is six bits. Type wc -c tiny.pbm in the terminal and see how big the file really is.

Where do the extra bytes come from? Look at cat tiny.pbm again and count every character, ends of lines included.

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.