Skip to content
Book 7: Computer Systems

Book 7 · Chapter 1

Data representation and logic gates

§1.1§1.2§1.3Free sample chapterExam craftB7·1

This chapter is being written.

Everything the site already has for it is below — each with the short code the book prints beside it.

§1.1 Number systems and logic gates

Short code B7·1·1

§1.2 Text, sound and images

Short code B7·1·2

§1.3 Data compression

Short code B7·1·3

Why computers use binary

Short code B7·1·10

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

The six logic gates

Short code B7·1·11

The XOR gate worked out by the Logic Gates Lab: the output column for inputs 00, 01, 10, 11.

Output

Truth table of X = A XOR B: output column (rows 00 to 11) = 0, 1, 1, 0.
Run and checked before printing

Measuring data storage

Short code B7·1·12

print(5 * 1024, "MiB in 5 GiB")
print(32768 // 8, "bytes in 32768 bits")
print(32768 // 8 // 1024, "KiB in 32768 bits")
Two conversions, one step per line. Notice bits → bytes uses 8, and every step above the byte uses 1024.

Output

5120 MiB in 5 GiB
4096 bytes in 32768 bits
4 KiB in 32768 bits
Run and checked before printing Run it in the lab

Denary and binary

Short code B7·1·13

Denary to binary by place values: the Number Systems Lab's working for 181.

Output

181 = 128 + 32 + 16 + 4 + 1 → put a 1 under each of those place values: 10110101
Run and checked before printing
n = 13
bits = ""
while n > 0:
    r = n % 2
    bits = str(r) + bits
    print(n, r, bits, n // 2)
    n = n // 2
print(bits)
Trace-it check — the denary-to-binary loop, one line per pass

Output

13 1 1 6
6 0 01 3
3 1 101 1
1 1 1101 0
1101
Run and checked before printing Run it in the lab

Hexadecimal

Short code B7·1·14

Hex to denary: each digit times its place value (16² = 256, 16¹ = 16, 16⁰ = 1).

Output

3C7: each hex digit is 4 bits — 3 = 3 = 0011, C = 12 = 1100, 7 = 7 = 0111
Denary: 3 × 16² + 12 × 16¹ + 7 × 16⁰ = 967
Run and checked before printing

Adding binary numbers

Short code B7·1·15

The same sum checked by the Binary Arithmetic lab.

Output

00101101 + 00110110 = 01100011 (45 + 54 = 99).
Run and checked before printing
Diagram check — the overflow figure

Output

11010110 + 01101011 = 01000001 — overflow (214 + 107; the true answer 321 does not fit in 8 bits).
Run and checked before printing

Logical shifts

Short code B7·1·16

A logical left shift of 2 places. The two 0s pushed off the left are lost; two 0s fill the right.

Output

Logical shift left 2 places: 00101101 → 10110100 (bits shifted out: 00).
Run and checked before printing
When 1s fall off the end, the answer is wrong: 101 × 8 should be 808, but the register holds 40.

Output

Logical shift left 3 places: 01100101 → 00101000 (bits shifted out: 011).
Run and checked before printing

Cyclic shifts

Short code B7·1·17

A cyclic left shift of 3 places: the first three bits come round to the right-hand end.

Output

Cyclic (rotate) left 3 places: 10110010 → 10010101.
Run and checked before printing

Two's complement

Short code B7·1·18

−37 in two's complement: positive pattern, flip, add 1, then check with −128.

Output

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

Text and character sets

Short code B7·1·19

Output

A 65 1000001
B 66 1000010
C 67 1000011
a 97 1100001
Run and checked before printing Run it in the lab

Sound

Short code B7·1·20

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

Bitmap images

Short code B7·1·21

Output

3C 42 A5 81 A5 99 42 3C
Run and checked before printing Run it in the lab

Why compress?

Short code B7·1·22

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

Run length encoding

Short code B7·1·23

log = "SSSSSRRRSSSSCC"
out = ""
count = 1
for i in range(1, len(log) + 1):
    if i < len(log) and log[i] == log[i - 1]:
        count = count + 1
    else:
        out = out + str(count) + log[i - 1]
        count = 1
print(out)
print(len(log), "characters became", len(out))
RLE in Python: count each run, then write the count and the character.

Output

5S3R4S2C
14 characters became 8
Run and checked before printing Run it in the lab

Huffman coding

Short code B7·1·24

Output

10000101001100111
17 bits instead of 72
Run and checked before printing Run it in the lab

Lossy or lossless?

Short code B7·1·25

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

Past-paper practice

Short code B7·1·90

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

Workbook: Data representation and logic gates

Short code W7·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 and logic gates
Enroll nowOnline classes