Everything becomes a number
A computer stores nothing but binary. Text, sound, pictures and video all have to be converted into numbers first, and the rule for doing so is a code agreed in advance so that two machines interpret the same bits identically.
Each kind of data has its own conversion, and each involves a trade-off between how faithful the result is and how much space it takes. That trade-off is the examinable idea running through the whole topic.
| Data | Converted by | Quality controlled by |
|---|---|---|
| Text | a character set — ASCII or Unicode | how many characters the set covers |
| Image | a grid of pixels, each a colour number | resolution and colour depth |
| Sound | measuring the wave at intervals | sample rate and sample resolution |
Text
A character set assigns a number to every character. ASCII uses 7 bits, giving 128 characters — enough for English letters, digits, punctuation and a few control codes. Extended ASCII uses 8 bits for 256.
That is not enough for the world's writing systems, so Unicode assigns numbers to over a million characters, covering Urdu, Arabic, Chinese, emoji and much else. Its first 128 code points are deliberately identical to ASCII, so old text remains readable.
The cost is size: a Unicode character may take up to four bytes where an ASCII one takes a single byte. UTF-8, the encoding used by almost every web page, keeps common English characters at one byte and spends more only where needed.
Letters are in order for a reason
In ASCII, A is 65 and B is 66, running consecutively to Z at 90; a is 97. Because the codes are consecutive, sorting alphabetically is just sorting numerically, and converting between upper and lower case is adding or subtracting 32. Neither would work if the assignments were arbitrary, and exam questions test exactly these consequences.
Images
A bitmap image is a grid of pixels, each storing a colour as a number. Two properties decide both the quality and the file size.
Resolution is the number of pixels — more pixels means finer detail. Colour depth is the number of bits per pixel — more bits means more distinguishable colours. 1 bit per pixel gives black and white only; 8 bits give 256 colours; 24 bits give about 16.7 million, which is more than the eye can distinguish and is why it is the common choice.
An image is 800 by 600 pixels with a colour depth of 24 bits. Calculate its file size in megabytes.
- Pixels
= 800 × 600 = 480 000.Resolution first — this is the number of colour values to be stored. - Bits
= 480 000 × 24 = 11 520 000.Each pixel needs 24 bits to record its colour. - Bytes
= 11 520 000 ÷ 8 = 1 440 000.Eight bits to the byte. Omitting this gives an answer eight times too large. - Megabytes
= 1 440 000 ÷ 1 048 576 = 1.37MB.Dividing by 1024 twice. Using 1 000 000 gives 1.44 MB, which is accepted if the convention is stated.
About 1.37 MB
Sound
Sound is a continuously varying wave, and a computer cannot store something continuous. Instead it measures the amplitude at regular instants — sampling — and stores each measurement as a number.
Two settings control the result. The sample rate is how many measurements are taken per second, in hertz; a higher rate follows the wave more closely in time. The sample resolution is the number of bits per measurement; more bits allow a closer match in amplitude. Both improve accuracy and both increase the file size, in direct proportion.
Drag sample rate alone and watch the samples follow the wave more closely in time. Now drag resolution alone — the dashed levels multiply and each sample lands nearer the true height. Two independent controls, both costing file size.
Rate and resolution are not the same thing
Sample rate is how often you measure; sample resolution is how precisely each measurement is recorded. A high rate with low resolution gives a signal measured frequently but crudely; the reverse gives precise measurements too far apart to capture the shape. Questions ask about them separately, and using one term for the other loses the mark.
Before you leave this chapter
- A character set assigns a number to each character: ASCII 7-bit, Unicode over a million.
- ASCII letters are consecutive, so sorting and case conversion are arithmetic.
- Image size = width × height × colour depth, in bits. Divide by 8 for bytes.
- Colour depth of n bits gives 2ⁿ colours.
- Sound size = sample rate × resolution × seconds. Rate is how often, resolution is how precisely.
Working out a file size
Almost every calculation question in this topic is the same chain, and writing it out as a chain is what keeps the marks even when one step goes wrong.
Find the number of items — pixels for an image, samples for a sound. Multiply by the bits each one takes. That gives bits. Divide by 8 for bytes, then by 1024 for kilobytes and again for megabytes.
| Step | Image | Sound |
|---|---|---|
| Count the items | width × height pixels | sample rate × seconds |
| Bits per item | colour depth | sample resolution |
| Multiply | pixels × depth | samples × resolution |
| Then | ÷ 8 for bytes | ÷ 8, and × 2 for stereo |
| Then | ÷ 1024 for KB, again for MB | the same |
The answer comes out in bits
Every one of these formulas produces a number of bits, because colour depth and sample resolution are both measured in bits. Forgetting to divide by 8 gives an answer exactly eight times too large, and it is the single most common error in the topic. Write the unit next to every intermediate value and the mistake becomes visible.