1 ยท Number Systems: Binary, Denary & Hexadecimal
Computers store everything as binary (base 2) โ only 0s and 1s โ because a circuit is either off (0) or on (1). Humans use denary (base 10). Hexadecimal (base 16) is a shorthand for binary that is easier to read.
Place values (8-bit binary)
Each bit has a value that doubles from right to left. To find the denary value, add the place values wherever there is a 1.
Denary โ Binary
Keep subtracting the largest place value that fits, writing a 1 each time, 0 otherwise.
Example: 77 โ 64 fits (1), 32 no (0), 16 no (0), 8 fits (1), 4 fits (1), 2 no (0), 1 fits (1) โ 01001101.
Binary โ Hex (the easy way)
Split the 8 bits into two groups of four. Each group of four bits = one hex digit (0โF). 0100 = 4, 1101 = D, so the byte is 4D.
10011100. Check: 128+16+8+4 = 156. โ1110 and 0110. 1110 = 8+4+2 = 14 = E. 0110 = 4+2 = 6. Answer: E6.2 ยท Logic Gates & Truth Tables
Logic gates are the building blocks of a processor. Each takes one or more binary inputs and gives a single binary output, following a fixed rule shown in a truth table.
Truth tables
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
Remember: AND = output 1 only if both inputs are 1. OR = output 1 if either input is 1. NOT = flips the input.
D AND S. The alarm output is 1 only when both D = 1 and S = 1.3 ยท Searching & Sorting
Linear search
Check each item one at a time from the start until the target is found or the list ends. Simple, works on any list, but slow for large lists.
FOR i <- 0 TO LENGTH(list) - 1
IF list[i] = target THEN
RETURN i // found at position i
ENDIF
NEXT i
RETURN -1 // not found
Bubble sort
Repeatedly step through the list, comparing each pair of neighbours and swapping them if they are in the wrong order. After each full pass the largest remaining value "bubbles" to the end.
FOR pass <- 1 TO LENGTH(list) - 1
FOR i <- 0 TO LENGTH(list) - 2
IF list[i] > list[i+1] THEN
// swap them
temp <- list[i]
list[i] <- list[i+1]
list[i+1] <- temp
ENDIF
NEXT i
NEXT pass
Try these in the Pseudocode IDE to watch them run.
[3, 5, 1, 8] (8 has bubbled to the end).