Skip to content
Book 5: Computer Systems

Book 5 · Chapter 1

Data representation

§1.1§1.2§1.3Free sample chapterBanker · ≈ 20.7 m · ↗B5·1

This chapter is free to read here.

Read it in full on cswithzak.com — every question with its mark scheme, every program ready to run. The free practice for each section is below, with the short code the book prints beside it.

Read it free

For the whole chapter

§1.1 Number systems · Binary arithmetic

Short code B5·1·1

§1.2 Text, sound and images

Short code B5·1·2

§1.3 Data storage and compression

Short code B5·1·3

Why computers use binary

Short code B5·1·10

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Binary and denary

Short code B5·1·11

Output

10110101: add the place values of the 1 bits: 128 + 32 + 16 + 4 + 1 = 181
Run and checked before printing

Output

92 = 0101 1100 in binary (8 bits).
Run and checked before printing

Output

1500 = 0000 0101 1101 1100 in binary (16 bits).
Run and checked before printing

Output

0010011100010000 = 10000 in denary.
Run and checked before printing

Hexadecimal

Short code B5·1·12

Output

1011011100101110 = B72E in hexadecimal.
Run and checked before printing

Output

2C9 = 0010 1100 1001 in binary (12 bits).
Run and checked before printing

Output

2C9: each hex digit is 4 bits — 2 = 2 = 0010, C = 12 = 1100, 9 = 9 = 1001
Denary: 2 × 16² + 12 × 16¹ + 9 × 16⁰ = 713
Run and checked before printing

Output

200 = 128 + 64 + 8 → put a 1 under each of those place values: 11001000
Hex: split into groups of 4 bits from the right — 1100 = 12 = C, 1000 = 8 = 8
Run and checked before printing

Why hexadecimal is useful

Short code B5·1·13

Output

FF = 255 in denary.
Run and checked before printing

Output

80 = 128 in denary.
Run and checked before printing

Adding binary numbers and overflow

Short code B5·1·14

Output

Add column by column from the right (0+0=0, 0+1=1, 1+1=0 carry 1, 1+1+1=1 carry 1):
  carry  1 1 1 1 1 1 0 0   (carry out of the left column: 0)
         0 1 1 0 1 1 1 0
       + 0 1 0 1 1 0 1 1
       = 1 1 0 0 1 0 0 1
  Column 1: 0 + 1 = 1 → write 1
  Column 2: 1 + 1 = 2 → write 0, carry 1
  Column 3: 1 + 0 + 1 (carry) = 2 → write 0, carry 1
  Column 4: 1 + 1 + 1 (carry) = 3 → write 1, carry 1
  Column 5: 0 + 1 + 1 (carry) = 2 → write 0, carry 1
  Column 6: 1 + 0 + 1 (carry) = 2 → write 0, carry 1
  Column 7: 1 + 1 + 1 (carry) = 3 → write 1, carry 1
  Column 8: 0 + 0 + 1 (carry) = 1 → write 1
Unsigned: 110 + 91 = 201 — no carry out of the left column, so no overflow.
(If the patterns are two's complement instead: 110 + 91 = 201, outside the 8-bit range −128 to 127 — OVERFLOW (the sign bit is wrong: 11001001 reads as −55).)
Run and checked before printing

Output

11001010 + 01110101 = 00111111 — overflow (202 + 117; the true answer 319 does not fit in 8 bits).
Run and checked before printing

Logical shifts

Short code B5·1·15

Output

Start: 00010110 (22)
Shift left: drop the 0 at the left, fill 0 on the right → 0010 1100
Shift left: drop the 0 at the left, fill 0 on the right → 0101 1000
Value 22 → 88: multiplied by 4
Run and checked before printing

Output

Start: 10110100 (180)
Logical shift right: drop the 0 at the right, fill 0 on the left → 0101 1010
Logical shift right: drop the 0 at the right, fill 0 on the left → 0010 1101
Logical shift right: drop the 1 at the right, fill 0 on the left → 0001 0110
Value 180 → 22: divided by 8 (rounded — a 1 bit was lost)
Run and checked before printing

Negative numbers: two's complement

Short code B5·1·16

Output

1101 0011 in 8-bit two's complement = −45.
Run and checked before printing

Output

45 in 8-bit binary: 32 + 8 + 4 + 1 → 0010 1101
Negative, so two's complement it: invert every bit → 1101 0010, then add 1 → 1101 0011
(Shortcut: keep the bits up to and including the first 1 from the right, flip the rest — same answer.)
Check: −128 + 64 + 16 + 2 + 1 = −45 (the left bit is worth −128)
Run and checked before printing

Output

150 cannot be stored in 8-bit two's complement (range −128 to 127).
Run and checked before printing

Text: character sets, ASCII and Unicode

Short code B5·1·17

for ch in "Hi!":
    code = ord(ch)
    print(ch, code, format(code, "08b"))
Each character of "Hi!" with its ASCII code in denary and in 8-bit binary.

Output

H 72 01001000
i 105 01101001
! 33 00100001
Run and checked before printing Run it in the lab
characters = {"letter A": "A", "Arabic beh": chr(1576),
              "euro sign": chr(8364), "grinning face": chr(128512)}
for name, ch in characters.items():
    code = ord(ch)
    print(name, code, code.bit_length(), "bits")
Four characters and the number of bits their Unicode codes need. Only the letter A fits in 7 bits.

Output

letter A 65 7 bits
Arabic beh 1576 11 bits
euro sign 8364 14 bits
grinning face 128512 17 bits
Run and checked before printing Run it in the lab

Sound: sampling

Short code B5·1·18

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Images: pixels, resolution and colour depth

Short code B5·1·19

for depth in [1, 2, 4, 8, 16, 24]:
    print(depth, "bits:", 2 ** depth, "colours")
Each extra bit of colour depth doubles the number of colours a pixel can be.

Output

1 bits: 2 colours
2 bits: 4 colours
4 bits: 16 colours
8 bits: 256 colours
16 bits: 65536 colours
24 bits: 16777216 colours
Run and checked before printing Run it in the lab

Measuring storage

Short code B5·1·20

size = 1024
for unit in ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]:
    print("1", unit, "=", size, "bytes")
    size = size * 1024
Each binary unit in bytes. Every step is another × 1024.

Output

1 KiB = 1024 bytes
1 MiB = 1048576 bytes
1 GiB = 1073741824 bytes
1 TiB = 1099511627776 bytes
1 PiB = 1125899906842624 bytes
1 EiB = 1152921504606846976 bytes
Run and checked before printing Run it in the lab

Calculating file sizes

Short code B5·1·21

Output

1024 × 768 image, 16-bit colour depth: 1,536 KiB
Run and checked before printing

Output

1024 × 768 image, 16-bit colour depth: 1.5 MiB
Run and checked before printing

Output

File size of a 120 s of sound at 8,192 Hz, 16-bit resolution:
  Samples = 8,192 Hz × 120 s = 983,040
  Bits = 983,040 × 16 (resolution) = 15,728,640 bits
  Bytes = 15,728,640 ÷ 8 = 1,966,080 bytes
  KiB = 1,966,080 bytes ÷ 1,024 (1 KiB = 1024 bytes) = 1,920 KiB
  (For comparison: 1,966.08 KB. Cambridge uses KiB/MiB for 1024-based units and kB/MB for 1000-based ones — answer in the unit the question names.)
Run and checked before printing

Output

File size of a 45 s of sound at 16,384 Hz, 8-bit resolution:
  Samples = 16,384 Hz × 45 s = 737,280
  Bits = 737,280 × 8 (resolution) = 5,898,240 bits
  Bytes = 5,898,240 ÷ 8 = 737,280 bytes
  KiB = 737,280 bytes ÷ 1,024 (1 KiB = 1024 bytes) = 720 KiB
  (For comparison: 737.28 KB. Cambridge uses KiB/MiB for 1024-based units and kB/MB for 1000-based ones — answer in the unit the question names.)
Run and checked before printing

Why compress files?

Short code B5·1·22

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Lossy and lossless compression, and RLE

Short code B5·1·23

def rle(data):
    pairs = []
    count = 1
    for i in range(1, len(data) + 1):
        if i < len(data) and data[i] == data[i - 1]:
            count = count + 1
        else:
            pairs.append(str(count) + data[i - 1])
            count = 1
    return " ".join(pairs)

for data in ["WWWWWWBBBWWWW", "ABCD"]:
    print(data, "->", rle(data))
Run-length encoding: a row of pixels with long runs, and a piece of text with none.

Output

WWWWWWBBBWWWW -> 6W 3B 4W
ABCD -> 1A 1B 1C 1D
Run and checked before printing Run it in the lab

Workbook: Data representation

Short code W5·1 · Computer Systems Workbook

The write-in workbook for this chapter — 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.

Ask Z about Data representation
Enroll nowOnline classes