Book 7 · Chapter 1
Data representation and logic gates
B7·1This chapter is being written.
Everything the site already has for it is below — each with the short code the book prints beside it.
For the whole chapter
- Topic pageData representation and logic gates (2029–2031)
B7·1·T - QuizData representation and logic gates
B7·1·Q1 - LabNumber Systems Lab — convert and add
B7·1·X1 - LabBinary arithmetic and shifts
B7·1·X2 - LabLogic Gates Lab
B7·1·X3 - LabPython example py-binary-hex
B7·1·X4 - LabPython example py-logic-gates
B7·1·X5 - LabData Lab — text, images and sound
B7·1·X6 - LabData Lab — compression (run length encoding)
B7·1·X7
§1.1 Number systems and logic gates
Short code B7·1·1
- FlashcardsFlashcards §1.1: Binary, logic gates & storage units
B7·1·F1 - FlashcardsFlashcards §1.1: Conversions, addition, shifts & two's complement
B7·1·F2
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
Output
Truth table of X = A XOR B: output column (rows 00 to 11) = 0, 1, 1, 0.
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")Output
5120 MiB in 5 GiB 4096 bytes in 32768 bits 4 KiB in 32768 bits
Denary and binary
Short code B7·1·13
Output
181 = 128 + 32 + 16 + 4 + 1 → put a 1 under each of those place values: 10110101
n = 13
bits = ""
while n > 0:
r = n % 2
bits = str(r) + bits
print(n, r, bits, n // 2)
n = n // 2
print(bits)Output
13 1 1 6 6 0 01 3 3 1 101 1 1 1 1101 0 1101
Hexadecimal
Short code B7·1·14
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
Adding binary numbers
Short code B7·1·15
Output
00101101 + 00110110 = 01100011 (45 + 54 = 99).
Output
11010110 + 01101011 = 01000001 — overflow (214 + 107; the true answer 321 does not fit in 8 bits).
Logical shifts
Short code B7·1·16
Output
Logical shift left 2 places: 00101101 → 10110100 (bits shifted out: 00).
Output
Logical shift left 3 places: 01100101 → 00101000 (bits shifted out: 011).
Cyclic shifts
Short code B7·1·17
Output
Cyclic (rotate) left 3 places: 10110010 → 10010101.
Two's complement
Short code B7·1·18
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)
Text and character sets
Short code B7·1·19
Output
A 65 1000001 B 66 1000010 C 67 1000011 a 97 1100001
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
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))Output
5S3R4S2C 14 characters became 8
Huffman coding
Short code B7·1·24
Output
10000101001100111 17 bits instead of 72
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.


