Encoding, Abstraction and Analog

Abstraction

Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details that are irrelevant to the question at hand, and bringing together the details that are related and useful, abstraction reduces complexity and lets you think about the idea instead of the machinery.
One type of abstraction we have already used is procedural abstraction, which provides a name for a procedure and allows it to be used knowing only what it does, not how it does it.

Bits are grouped to represent abstractions

Those abstractions include, but are not limited to, numbers, characters and colour.
In the two lines below, sequences of 0s and 1s represent a string of characters and a numeric grade. A colour is built the same way, out of three numbers: how much red, how much green and how much blue, each one a whole number from 0 to 255.
name = "Mike Smith"
grade = 87
None of that storage is visible in the code. Underneath, both lines are nothing but 0s and 1s.

Encoding

A computer cannot store letters or pictures. It can only work with bits, a 0 or a 1.
To represent anything other than bits, we need rules that let us convert a sequence of bits into letters or pictures. That set of rules is called an encoding scheme, or an encoding for short. Here is one sequence of bits, read under the rules of ASCII:
01100010 01101001 01110100 01110011
b i t s

ASCII

ASCII (American Standard Code for Information Interchange) is an encoding scheme that specifies the mapping between bits and characters. There are 128 characters in the scheme. Of those, 95 are printable, starting with the space at code point 32. The other 33 are control codes that print nothing, such as tab, newline and backspace: code points 0 to 31, and 127.
The printable characters include a-z, A-Z, 0-9 and punctuation. In ASCII, 65 represents A, 66 represents B, and so on. Those numbers are called code points.
A beige Teletype Model 33 in a museum: a keyboard below a printer that types on a roll of paper.
A Teletype Model 33, a printing terminal from the 1960s that sent and received characters as ASCII codes. Photo: ArnoldReinhold, CC BY-SA 3.0, via Wikimedia Commons.
The 128 ASCII code points as a bar from 0 to 127. 0 to 31 are control codes; 32 is the space; 32 to 126 are printable; 127 is delete. Marked below the bar: 48 is the digit 0, 65 is A, 97 is a. Key: 33 control codes (0 to 31, and 127): tab, newline, backspace and the rest. They print nothing. 95 printable characters (32 to 126): the space, punctuation, digits and letters.
bitscode pointcharacter
0100000165A
0100001066B
0100001167C
0100010068D
0100010169E
0100011070F
The program below writes the code point of each character of a word in binary. ord gives the code point, a divide-by-two loop writes it in binary, and zeros are added on the left to make eight bits. Run it, then change it and run it again:
• Change "Owl" to your own name.
• Change the 8 in len(bits) < 8 to 7.
word = "Owl"
for letter in word:
    code = ord(letter)
    bits = ""
    value = code
    while value > 0:
        bits = str(value % 2) + bits
        value = value // 2
    while len(bits) < 8:
        bits = "0" + bits
    print(letter, code, bits)
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

Unicode is not an encoding

So how many bits does Unicode use to encode all these characters? None. Because Unicode is not an encoding.
Unicode first and foremost defines a table of code points for characters. That is a fancy way of saying 65 stands for A, 66 stands for B, and 9,731 stands for a snowman.
To represent 1,114,112 different values, two bytes are not enough. Three bytes are, but three bytes are often awkward to work with, so four bytes would be the comfortable minimum.

The UTF encodings

UTF-32, UTF-16 and UTF-8 are three encodings that use the Unicode table of code points. Unicode says which number a character is. A UTF encoding says how that number is written as bits.
UTF-32 encodes every code point in 32 bits, four bytes per character. It is very simple, and it often wastes a lot of space: an English document bloats to four times its necessary size, most of the extra being zeros.
UTF-16 and UTF-8 are variable-length encodings. If a character can be represented in a single byte, because its code point is a very small number, UTF-8 encodes it in a single byte. If it needs two, UTF-8 uses two, and so on. UTF-16 sits in the middle, using at least two bytes and growing to four as necessary.
Of the three, UTF-8 is by far the most widely used. It is the standard encoding for email and for webpages.
characterUTF-8UTF-16UTF-32
A1 byte2 bytes4 bytes
あ3 bytes2 bytes4 bytes

Digital and analog

Computers can only understand digital data: discrete values, like a list of integers or a sequence of 0s and 1s.
Analog data have values that change smoothly, rather than in discrete intervals, over time. Temperatures over a period of time, the pitch and volume of music, the colours of a painting and the position of a sprinter during a race are all analog.
Analog signals are continuous and can take on infinitely many values, the real numbers. Digital signals are finite: an 8-bit colour is one of 256 discrete possibilities, while an actual colour can be any of infinitely many shades.
A black seven-inch vinyl record with a blue label next to a smaller silver compact disc, on a white background.
A vinyl record and a CD. The record's groove is a continuous copy of the sound wave; the CD stores the sound as samples, each a number. Photo: Dillan Payne, CC BY-SA 4.0, via Wikimedia Commons.
One analog signal, sampled at two rates. Low rate: 9 samples. The stored samples, held flat until the next one, form a coarse staircase that misses most of the curve's bumps. Higher rate: 36 samples over the same stretch. The staircase follows the curve closely. Gray line: the analog signal. Blue dots and steps: samples, as stored. Each sample is rounded to one of 16 levels (4 bits), the faint lines.

Bits per sample

Each sample is a measurement, stored using a set number of bits. That number of bits decides how many different values a sample can take. CD audio stores each sample in 16 bits, so a sample can be any of 2^16 = 65,536 levels. A true value that falls between two levels is rounded to the nearer one. The figure above gives each sample only 4 bits, 16 levels, so the rounding is easy to see.
Using digital data to approximate analog data in this way is an example of abstraction. The samples keep the details that matter, how the signal rises and falls, and hide the infinitely many values in between, which no file could hold.