Math and Logic Refresher
May 16, 2026·11 min read·intermediate
This appendix collects the mathematical and logical background used throughout the book. Most readers will know much of it; the appendix exists so that any reader who needs to refresh a particular…
This appendix collects the mathematical and logical background used throughout the book. Most readers will know much of it; the appendix exists so that any reader who needs to refresh a particular topic can do so without leaving the volume. The treatment is concise — for a deeper introduction, any standard discrete-mathematics text covers the same material at length.
01. Number Systems
Computers operate in binary. Humans usually think in decimal. Hexadecimal (base 16) is the working notation for engineers because each hex digit corresponds to exactly four binary bits, so hex compactly represents binary values without losing the bit-level structure.
Bases
A number written in base with digits has value:
For binary, and digits are . For hex, and digits are where through .
Examples:
- Binary
1011= in decimal. - Hex
2F= in decimal. - Hex
FF= . - Hex
100= 256;1000= 4096;10000= 65,536.
Common Conventions
In this book and most code:
- Binary is written with prefix
0b(e.g.,0b1011) or with subscript (). - Hex is written with prefix
0x(e.g.,0x2F) or with subscript (). - Decimal has no prefix.
In assembly, hex sometimes uses suffix h (Intel: 0FFh) or prefix $ (Motorola).
Conversions
Binary to hex: group from the right in 4-bit chunks; each chunk is one hex digit.
| Binary: 1101 0010 1111 0110 | |
| Hex: D 2 F 6 -> 0xD2F6 |
Hex to binary: expand each hex digit to 4 bits.
| Hex: 0xA5 | |
| Binary: 1010 0101 |
Decimal to binary: repeatedly divide by 2; the remainders read in reverse give the binary representation.
| Convert 13 to binary: | |
| 13 / 2 = 6 r 1 | |
| 6 / 2 = 3 r 0 | |
| 3 / 2 = 1 r 1 | |
| 1 / 2 = 0 r 1 | |
| Read remainders bottom-up: 1101 |
Binary to decimal: sum powers of 2 corresponding to set bits.
| 1101 = 8 + 4 + 1 = 13 |
Powers of 2 to Memorize
| Power | Value | Common name |
|---|---|---|
| 1,024 | KiB | |
| 65,536 | 64 KiB / 16-bit range | |
| 1,048,576 | MiB | |
| ≈ | GiB | |
| ≈ 4.3 × | 32-bit address space | |
| ≈ | TiB | |
| ≈ 2.8 × | typical 64-bit virtual address space | |
| ≈ | PiB | |
| ≈ 1.8 × | full 64-bit range |
Note the IEC distinction: KiB = = 1024 bytes; KB sometimes means = 1000 bytes (storage marketing). Memory and cache sizes always use binary (KiB/MiB), even when written as "KB"/"MB".
02. Two's Complement
Computers represent signed integers in two's complement. In an -bit two's complement representation:
- The most significant bit has weight (negative).
- Other bits have positive weights.
- The range is .
For 8-bit:
| Bit pattern | Unsigned | Two's complement |
|---|---|---|
00000000 | 0 | 0 |
00000001 | 1 | 1 |
01111111 | 127 | 127 |
10000000 | 128 | -128 |
10000001 | 129 | -127 |
11111111 | 255 | -1 |
Computing Negation
To negate a two's complement number: invert all bits, then add 1.
| +5 in 8-bit: 0000 0101 | |
| Invert: 1111 1010 | |
| Add 1: 1111 1011 | |
| = -5 |
Verify: . ✓
Why Two's Complement
The same hardware adder works for unsigned and signed addition; the result is the correct two's complement value (modulo ). Subtraction is just adding the negation. Only one representation of zero. These properties make two's complement universal in modern hardware.
Sign Extension and Zero Extension
When extending a smaller value to a larger width:
- Zero-extend: pad with zeros on the left (preserves unsigned value).
- Sign-extend: pad with copies of the MSB on the left (preserves signed value).
| 8-bit -5 = 11111011 | |
| Zero-extend to 16-bit: 00000000 11111011 (= 251 unsigned, wrong if interpreted as signed) | |
| Sign-extend to 16-bit: 11111111 11111011 (= -5 signed) ✓ |
x86-64 has explicit MOVZX (zero) and MOVSX (sign) instructions; AArch64 has UXTB/SXTB etc. RISC-V has LB (sign-extend) and LBU (zero-extend).
Overflow
Two's complement addition overflows when the result has a different sign than both inputs (and the inputs have the same sign).
- Positive + positive = negative → overflow.
- Negative + negative = positive → overflow.
- Positive + negative → no overflow possible.
Hardware sets a flag (V on x86, V on ARM); on RISC-V there is no flag, so signed-overflow detection is done with a comparison.
03. Boolean Algebra
Digital logic is built on Boolean algebra over .
Basic Operators
| Operator | Symbol | Truth |
|---|---|---|
| AND | or | 1 only if both 1 |
| OR | or | 1 if either or both 1 |
| NOT | or | 1 if is 0 |
| XOR | 1 if exactly one of , is 1 | |
| NAND | NOT(AND) | |
| NOR | NOT(OR) |
Identities
Commutative: ; .
Associative: .
Distributive: .
Identity: ; .
Annihilator: ; .
Idempotent: ; .
Complement: ; .
Double negation: .
De Morgan's Laws
These convert AND to OR and vice versa under negation. Heavily used in simplifying logic and reasoning about predicates in code.
Bit-level Operations in Code
In C/C++/Rust/Java/Go, bitwise operators work on integer types element by element:
&AND|OR^XOR~NOT<<shift left>>shift right (arithmetic for signed, logical for unsigned in C; language-dependent)
Common idioms:
| x | (1 << n) // set bit n | |
| x & ~(1 << n) // clear bit n | |
| x ^ (1 << n) // toggle bit n | |
| (x >> n) & 1 // test bit n | |
| x & (x - 1) // clear lowest set bit | |
| x & -x // isolate lowest set bit | |
| popcount(x) // count of set bits (instruction: POPCNT/CNT) |
04. Sets and Functions
Sets
A set is an unordered collection of distinct elements, written . Operations: union , intersection , difference , complement.
Cardinality is the number of elements. Power set has elements.
Functions
A function maps each element of to exactly one element of . Important properties:
- Injective (one-to-one): different inputs give different outputs.
- Surjective (onto): every output is hit by some input.
- Bijective: both injective and surjective; has an inverse.
Hash functions in caches and translation lookaside buffers are non-bijective: many inputs map to the same output (collisions). Designing the hash to distribute inputs uniformly is important for cache performance.
05. Combinatorics for Architecture
Counting Principles
Multiplication rule: if there are ways for one event and ways for another, there are ways for both.
Addition rule: if events are mutually exclusive, count separately and add.
A 4-way set-associative cache with 1024 sets has entries.
Permutations and Combinations
Permutations (order matters): .
Combinations (order doesn't matter): .
For the birthday paradox (relevant to hash collisions): in a set of items, the expected number of trials before a collision is roughly . For a 32-bit hash, expect a collision after roughly insertions.
Pigeonhole Principle
If items are placed in buckets, at least one bucket has 2 or more items. Useful for proving cache-conflict guarantees.
06. Probability for Architecture
Basic Probability
For a discrete event with possible outcomes with probabilities (summing to 1), the expected value of a quantity is:
Branch prediction and cache behavior are analyzed in expectation.
Cache Hit/Miss
If the hit ratio is and a hit costs while a miss costs , the average memory access time is:
For a multi-level cache:
Branch Prediction Accuracy
If the branch is correctly predicted with probability and mispredicting costs cycles, the expected penalty per branch is . For typical and , the expected penalty is cycles per branch.
Geometric Distribution
The expected number of trials to get a single success when each trial has probability is . Used in modeling rare events (TLB miss after how many memory accesses, etc.).
Independence
Events and are independent if . In real workloads, cache misses and branch mispredicts are often correlated (both happen on irregular access patterns), so naive independence assumptions are optimistic.
07. Asymptotic Notation (Big-O)
Used to describe how cost scales with input size .
- : upper bound. The actual cost grows no faster than asymptotically.
- : lower bound.
- : tight bound (both upper and lower).
Common growth rates, in increasing order:
For computer architecture analysis:
- Cache size scaling: a fully-associative LRU cache has tag-comparison cost per access.
- Set-associative cache: comparisons per access (constant w.r.t. cache size).
- TLB walk: memory accesses (4-5 in modern systems).
- Sorting (relevant to data layout for cache): merge sort is in time but in extra space; in-place quicksort is also on average.
Constants Matter in Practice
Big-O ignores constants, but architecture is largely about constants. An algorithm that does cache-friendly sequential access can outperform a "better" algorithm that pointer-chases through memory. Cache-aware analysis (the external memory model and the cache-oblivious model) is the formal way to capture this — counting cache-line transfers rather than just operations.
08. Logarithms and Exponentials
For computer architecture, base-2 logarithms dominate.
A cache with sets needs index bits. A 4 KiB page (4096 = bytes) has 12 offset bits.
The change of base identity: .
For asymptotic analysis the base doesn't matter (logs in different bases differ only by a constant), so we usually write without specifying base.
09. Summary
This appendix has reviewed:
- Number systems: binary, decimal, hex; conversions; common powers of 2.
- Two's complement: representation, negation, sign extension, overflow.
- Boolean algebra: operators, identities, De Morgan's laws, bit-level idioms.
- Sets, functions, combinatorics, the pigeonhole principle.
- Probability: expected values, AMAT, branch-prediction penalty.
- Asymptotic notation: big-O and where constants matter.
- Logarithms.
These are the recurring tools in architectural analysis. Specific applications appear throughout the book: cache analysis (Ch 18-19), branch prediction (Ch 24, 51), TLB hierarchy (Ch 17), instruction encoding (Ch 11-15), and microarchitecture timing (Part V).