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
| Word | What it means | Example |
|---|---|---|
| Character code | the number that stands for one letter or sign | 65 for A |
| Unicode | one list of codes for the letters of every language | |
| Pixel | one dot of colour in a picture | |
| Colour depth | how many bits each pixel uses | |
| Sample rate | how many times a second a sound is measured | |
| Bit depth | how 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
cd ~/archShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask Python for three codes
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
echo Hi > hi.txtShown 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
od -t u1 hi.txtShown 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
wc -c hi.txtShown 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
| Picture | Pixels | Bits per pixel | Bits | Bytes |
|---|---|---|---|---|
| 3 by 2, black and white | 6 | 1 | 6 | less than 1 |
| 100 by 50, 256 colours | 5,000 | 8 | 40,000 | 5,000 |
| 800 by 600, full colour | 480,000 | 24 | 11,520,000 | 1,440,000 |
The cells students fill in are left blank in this preview.
Write a picture of six pixels
printf 'P1\n3 2\n1 0 1\n0 1 0\n' > tiny.pbmShown 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
file tiny.pbmShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the picture as text
cat tiny.pbmShown 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
| Step | Sum | Answer |
|---|---|---|
| Samples a second | 44,100 | 44,100 |
| Bits a second, 16 bits, stereo | 44,100 × 16 × 2 | 1,411,200 |
| Bits in 60 seconds | 1,411,200 × 60 | 84,672,000 |
| Bytes | 84,672,000 ÷ 8 | 10,584,000 |
The cells students fill in are left blank in this preview.
Let Python do the sum
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.