Information encoding and the binary system
A computer represents text, images and sound with zeros and ones. We study bits and bytes, the binary and hexadecimal number systems, and the ASCII and Unicode encodings.
All topics (1 of 3)
Chapter 1. Introduction to information technology
Chapter 2. Programming fundamentals
A computer represents any information — a number, text, an image, sound — with just two symbols, 0 and 1. The reason is simple: in an electronic device it is more reliable to distinguish and store two states (for example, voltage present or absent). One binary digit is called a bit, and 8 bits make a byte. n bits can encode 2^n different states, so 1 byte expresses 256 values (from 0 to 255).
The binary number system is positional: the value of a digit depends on its place in the number. In the decimal system the places correspond to powers of 10 (1, 10, 100…), and in the binary system to powers of 2 (1, 2, 4, 8, 16…). For example, the number 1101 equals 1 × 8 + 1 × 4 + 0 × 2 + 1 × 1 = 13. To convert a decimal number to binary, divide it by 2 repeatedly and read the remainders in reverse order: for 13 the remainders are 1, 0, 1, 1, and in reverse order they give 1101.
To shorten a long binary notation, the hexadecimal system is used: each hexadecimal digit (0–9, A–F) corresponds to exactly 4 bits, for example 1010 1111 = AF. Python has these operations built in: bin(13) gives 0b1101, int("1101", 2) gives 13, and hex(175) gives 0xaf.
Text is also encoded with numbers: every character gets a numeric code. The ASCII table covers 128 characters (Latin letters, digits, punctuation and control characters); for example, the letter A has code 65. Unicode assigns a separate code to the characters of almost all writing systems in the world, and the UTF-8 encoding uses 1 to 4 bytes for each character: ASCII characters take 1 byte and Cyrillic letters take 2. An image is encoded as a grid of pixels: with 24-bit color each pixel takes 3 bytes (red, green and blue components).
Key takeaways
- A computer represents all information as a sequence of
0and1, that is, bits. - 8 bits = 1 byte; n bits encode 2^n different values.
- The binary system is positional: each place corresponds to a power of two (1, 2, 4, 8…).
- When converting from decimal to binary, the remainders of division by 2 are read in reverse order.
- Text is encoded with ASCII and Unicode (UTF-8) codes, and an image with numbers giving the color of pixels.
Practice tasks
- Convert 25, 100 and 255 to binary and
101101and11110000to decimal by hand, then check the results with Python’sbin()andint()functions. - Using
ord()andchr()in Python, find the code of each letter in your name and print it in binary withbin(). - Calculate how many bytes an uncompressed 24-bit color image of 1280 × 720 pixels occupies and convert the result to megabytes (take 1 MB = 1,048,576 bytes).
Review questions
Test yourself: think first, then reveal the answer.
1How many different values can be encoded with 8 bits, and why?Show answerHide answer
256: each bit has two values, so 2^8 = 256.2What is the binary number `1011` in decimal?Show answerHide answer
11, because 8 + 0 + 2 + 1 = 11.3Why do computers use the binary number system?Show answerHide answer
In an electronic device it is more reliable to distinguish and store two states (for example, voltage present or absent).
0 of 3 topics completed
Sign in to mark