Book 1 · Chapter 1
Information representation
B1·1This chapter is in the ebook.
Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.
For the whole chapter
- Topic pageInformation Representation
B1·1·T - QuizInformation Representation
B1·1·Q1 - LabNumber Systems Lab
B1·1·X1 - LabBinary arithmetic, BCD & shifts
B1·1·X2 - LabFile sizes & compression (Data Lab)
B1·1·X3
§1.1 Data representation · Binary arithmetic
Short code B1·1·1
- FlashcardsFlashcards §1.1: Number systems, BCD & two's complement
B1·1·F1 - Past-paper questionsPast-paper questions §1.1 (data-representation)
- Past-paper questionsPast-paper questions §1.1 (binary-arithmetic)
§1.2 Multimedia
Short code B1·1·2
- FlashcardsFlashcards §1.2: Multimedia — graphics & sound
B1·1·F2 - Past-paper questionsPast-paper questions §1.2 (multimedia)
B1·1·2
§1.3 Compression
Short code B1·1·3
- FlashcardsFlashcards §1.3: Compression
B1·1·F3 - Past-paper questionsPast-paper questions §1.3 (compression)
B1·1·3
Binary prefixes: kibi or kilo?
Short code B1·1·10
pairs = [("kilo", "kibi"), ("mega", "mebi"),
("giga", "gibi"), ("tera", "tebi")]
for n, (dec, bin_) in enumerate(pairs, start=1):
d = 1000 ** n
b = 1024 ** n
gap = (b - d) / d * 100
print(f"{dec}: {d:,} {bin_}: {b:,} (+{gap:.1f}%)")Output
kilo: 1,000 kibi: 1,024 (+2.4%) mega: 1,000,000 mebi: 1,048,576 (+4.9%) giga: 1,000,000,000 gibi: 1,073,741,824 (+7.4%) tera: 1,000,000,000,000 tebi: 1,099,511,627,776 (+10.0%)
drive = 2 * 1000 ** 4 # "2 TB" box
print(f"{drive:,} bytes")
print(f"{drive / 1024 ** 4:.2f} TiB")Output
2,000,000,000,000 bytes 1.82 TiB
Number bases: binary, denary and hexadecimal
Short code B1·1·11
DECLARE Number : INTEGER
DECLARE Bits : STRING
Number ← 181
Bits ← ""
WHILE Number > 0
IF Number MOD 2 = 1 THEN
Bits ← "1" & Bits
ELSE
Bits ← "0" & Bits
ENDIF
Number ← Number DIV 2
ENDWHILE
OUTPUT BitsOutput
10110101
DECLARE Number : INTEGER
DECLARE Remainder : INTEGER
DECLARE Hex : STRING
DECLARE Digits : STRING
Digits ← "0123456789ABCDEF"
Number ← 967
Hex ← ""
WHILE Number > 0
Remainder ← Number MOD 16
Hex ← MID(Digits, Remainder + 1, 1) & Hex
Number ← Number DIV 16
ENDWHILE
OUTPUT HexMID picks the hexadecimal digit for each remainder out of Digits, the string of all sixteen digits.Output
3C7
Binary Coded Decimal
Short code B1·1·12
n = 181
print("binary ", format(n, "08b"))
print("hex ", format(n, "X"))
print("BCD ", " ".join(format(int(d), "04b") for d in str(n)))Output
binary 10110101 hex B5 BCD 0001 1000 0001
Negative numbers: one's and two's complement
Short code B1·1·13
def read_twos(bits):
value = int(bits, 2)
if bits[0] == "1":
value = value - 256
return value
def write_twos(n):
return format(n % 256, "08b")
print(read_twos("10110110"))
print(write_twos(-74))
print(read_twos("01111111"))
print(read_twos("10000000"))Output
-74 10110110 127 -128
plus = format(74, "08b")
ones = "".join("1" if b == "0" else "0" for b in plus)
twos = format(int(ones, 2) + 1, "08b")
print("+74 ", plus)
print("one's complement ", ones)
print("two's complement ", twos)Output
+74 01001010 one's complement 10110101 two's complement 10110110
Binary addition and overflow
Short code B1·1·14
a = 0b10110110 # 182
b = 0b01100111 # 103
total = a + b
print(format(total, "09b"), "=", total)
low8 = total & 0xFF # keep 8 bits
print("8-bit result:", format(low8, "08b"))
print("overflow:", total > 255)Output
100011101 = 285 8-bit result: 00011101 overflow: True
Binary subtraction
Short code B1·1·15
a = 0b01011100 # 92
b = 0b00110101 # 53
neg_b = (~b + 1) & 0xFF # -53
raw = a + neg_b
ans = raw & 0xFF # drop bit 9
print("-53 ", format(neg_b, "08b"))
print("sum ", format(raw, "09b"))
print("answer", format(ans, "08b"), "=", ans)Output
-53 11001011 sum 100100111 answer 00100111 = 39
Where hexadecimal and BCD earn their place
Short code B1·1·16
count = 2047
for digit in str(count):
print(digit, "->", format(int(digit), "04b"))
print("patterns never used:", [format(v, "04b") for v in range(10, 16)])Output
2 -> 0010 0 -> 0000 4 -> 0100 7 -> 0111 patterns never used: ['1010', '1011', '1100', '1101', '1110', '1111']
Characters in binary: ASCII, extended ASCII and Unicode
Short code B1·1·17
for ch in ["A", "B", "Z", "a", "0", " "]:
code = ord(ch)
print(repr(ch), code, format(code, "07b"))Output
'A' 65 1000001 'B' 66 1000010 'Z' 90 1011010 'a' 97 1100001 '0' 48 0110000 ' ' 32 0100000
letters = ["A", "\u00e9", "\u0628", "\u20ac"]
for ch in letters:
n = ord(ch)
print(f"U+{n:04X}", n, n.bit_length())Output
U+0041 65 7 U+00E9 233 8 U+0628 1576 11 U+20AC 8364 14
Bitmap images: pixels, resolution and colour depth
Short code B1·1·18
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
How big is a bitmap?
Short code B1·1·19
width, height, depth = 1024, 768, 16
bits = width * height * depth
size_bytes = bits // 8
print(bits, "bits")
print(size_bytes, "bytes")
print(size_bytes // 1024, "KiB")
print(768 * 2, "KiB (cancelled by hand)")Output
12582912 bits 1572864 bytes 1536 KiB 1536 KiB (cancelled by hand)
Changing resolution and colour depth
Short code B1·1·20
def mib(w, h, depth):
return w * h * depth / 8 / 1024 ** 2
print(round(mib(4000, 3000, 24), 2), "MiB")
print(round(mib(2000, 1500, 24), 2), "MiB")
print(round(mib(4000, 3000, 8), 2), "MiB")
print(2 ** 24, "colours vs", 2 ** 8)Output
34.33 MiB 8.58 MiB 11.44 MiB 16777216 colours vs 256
Vector graphics
Short code B1·1·21
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Bitmap or vector?
Short code B1·1·22
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Sound: from wave to numbers
Short code B1·1·23
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Sampling rate and sampling resolution
Short code B1·1·24
for resolution in [2, 4, 8, 16]:
print(f"{resolution:>2} bits: {2 ** resolution:,} levels")Output
2 bits: 4 levels 4 bits: 16 levels 8 bits: 256 levels 16 bits: 65,536 levels
How big is a sound file?
Short code B1·1·25
rate = 44_100 # samples per second
resolution = 16 # bits per sample
seconds = 60
bits = rate * resolution * seconds
print(bits, "bits")
print(bits // 8, "bytes")
print(f"{bits / 8 / 1024 ** 2:.2f} MiB")Output
42336000 bits 5292000 bytes 5.05 MiB
rate = 32 * 1000 # 32 kHz in Hz
resolution = 16 # bits per sample
seconds = 2 * 60 # 2 minutes
bits = rate * resolution * seconds
print(bits, "bits")
print(bits // 8, "bytes")
print(bits / 8 / 1000 / 1000, "MB")Output
61440000 bits 7680000 bytes 7.68 MB
Why compress? Lossy and lossless
Short code B1·1·26
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Run-length encoding and compressing each kind of file
Short code B1·1·27
row = "WWWWWWBBBWWWWRRRRRRR"
runs = []
for colour in row:
if runs and runs[-1][1] == colour:
runs[-1][0] += 1
else:
runs.append([1, colour])
print(runs)
print(len(row), "pixels,", len(runs), "runs")Output
[[6, 'W'], [3, 'B'], [4, 'W'], [7, 'R']] 20 pixels, 4 runs
row = "WBWBWBWB"
runs = []
for colour in row:
if runs and runs[-1][1] == colour:
runs[-1][0] += 1
else:
runs.append([1, colour])
print(len(row), "values before")
print(2 * len(runs), "values after")Output
8 values before 16 values after
Workbook 1: Information representation
Short code W1·1 · Theory Fundamentals Workbook
The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.


