Part VIIIAppendices

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 bb with digits dndn1d1d0d_n d_{n-1} \ldots d_1 d_0 has value:

i=0ndibi\sum_{i=0}^{n} d_i \cdot b^i

For binary, b=2b = 2 and digits are {0,1}\{0, 1\}. For hex, b=16b = 16 and digits are {0,1,,9,A,B,C,D,E,F}\{0, 1, \ldots, 9, A, B, C, D, E, F\} where A=10A = 10 through F=15F = 15.

Examples:

  • Binary 1011 = 18+04+12+11=111 \cdot 8 + 0 \cdot 4 + 1 \cdot 2 + 1 \cdot 1 = 11 in decimal.
  • Hex 2F = 216+15=472 \cdot 16 + 15 = 47 in decimal.
  • Hex FF = 1516+15=25515 \cdot 16 + 15 = 255.
  • 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 (101121011_2).
  • Hex is written with prefix 0x (e.g., 0x2F) or with subscript (2F162F_{16}).
  • 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.

Code
Binary: 1101 0010 1111 0110
Hex: D 2 F 6 -> 0xD2F6

Hex to binary: expand each hex digit to 4 bits.

Code
Hex: 0xA5
Binary: 1010 0101

Decimal to binary: repeatedly divide by 2; the remainders read in reverse give the binary representation.

Code
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.

Code
1101 = 8 + 4 + 1 = 13

Powers of 2 to Memorize

PowerValueCommon name
2102^{10}1,024KiB
2162^{16}65,53664 KiB / 16-bit range
2202^{20}1,048,576MiB
2302^{30}10910^9GiB
2322^{32}≈ 4.3 × 10910^932-bit address space
2402^{40}101210^{12}TiB
2482^{48}≈ 2.8 × 101410^{14}typical 64-bit virtual address space
2502^{50}101510^{15}PiB
2642^{64}≈ 1.8 × 101910^{19}full 64-bit range

Note the IEC distinction: KiB = 2102^{10} = 1024 bytes; KB sometimes means 10310^3 = 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 nn-bit two's complement representation:

  • The most significant bit has weight 2n1-2^{n-1} (negative).
  • Other bits have positive weights.
  • The range is [2n1,2n11][-2^{n-1}, 2^{n-1} - 1].

For 8-bit:

Bit patternUnsignedTwo's complement
0000000000
0000000111
01111111127127
10000000128-128
10000001129-127
11111111255-1

Computing Negation

To negate a two's complement number: invert all bits, then add 1.

Code
+5 in 8-bit: 0000 0101
Invert: 1111 1010
Add 1: 1111 1011
= -5

Verify: 128+64+32+16+8+0+2+1=5-128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = -5. ✓

Why Two's Complement

The same hardware adder works for unsigned and signed addition; the result is the correct two's complement value (modulo 2n2^n). 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).
Code
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 {0,1}\{0, 1\}.

Basic Operators

OperatorSymbolTruth
ANDABA \cdot B or ABA \land B1 only if both 1
ORA+BA + B or ABA \lor B1 if either or both 1
NOTA\overline{A} or ¬A\lnot A1 if AA is 0
XORABA \oplus B1 if exactly one of AA, BB is 1
NANDAB\overline{A \cdot B}NOT(AND)
NORA+B\overline{A + B}NOT(OR)

Identities

Commutative: A+B=B+AA + B = B + A; AB=BAA \cdot B = B \cdot A.

Associative: A+(B+C)=(A+B)+CA + (B + C) = (A + B) + C.

Distributive: A(B+C)=AB+ACA \cdot (B + C) = A \cdot B + A \cdot C.

Identity: A+0=AA + 0 = A; A1=AA \cdot 1 = A.

Annihilator: A+1=1A + 1 = 1; A0=0A \cdot 0 = 0.

Idempotent: A+A=AA + A = A; AA=AA \cdot A = A.

Complement: A+A=1A + \overline{A} = 1; AA=0A \cdot \overline{A} = 0.

Double negation: A=A\overline{\overline{A}} = A.

De Morgan's Laws

AB=A+B\overline{A \cdot B} = \overline{A} + \overline{B} A+B=AB\overline{A + B} = \overline{A} \cdot \overline{B}

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:

C
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 {a,b,c}\{a, b, c\}. Operations: union \cup, intersection \cap, difference \setminus, complement.

Cardinality A|A| is the number of elements. Power set P(A)\mathcal{P}(A) has 2A2^{|A|} elements.

Functions

A function f:ABf: A \to B maps each element of AA to exactly one element of BB. 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 aa ways for one event and bb ways for another, there are aba \cdot b ways for both.

Addition rule: if events are mutually exclusive, count separately and add.

A 4-way set-associative cache with 1024 sets has 10244=40961024 \cdot 4 = 4096 entries.

Permutations and Combinations

Permutations (order matters): P(n,k)=n!(nk)!P(n, k) = \frac{n!}{(n-k)!}.

Combinations (order doesn't matter): (nk)=n!k!(nk)!\binom{n}{k} = \frac{n!}{k!(n-k)!}.

For the birthday paradox (relevant to hash collisions): in a set of nn items, the expected number of trials before a collision is roughly nπ/2\sqrt{n \cdot \pi / 2}. For a 32-bit hash, expect a collision after roughly 2162^{16} insertions.

Pigeonhole Principle

If n+1n+1 items are placed in nn 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 1,,k1, \ldots, k with probabilities pip_i (summing to 1), the expected value of a quantity XX is:

E[X]=ipiXiE[X] = \sum_i p_i \cdot X_i

Branch prediction and cache behavior are analyzed in expectation.

Cache Hit/Miss

If the hit ratio is hh and a hit costs ThitT_{\text{hit}} while a miss costs TmissT_{\text{miss}}, the average memory access time is:

AMAT=hThit+(1h)Tmiss\text{AMAT} = h \cdot T_{\text{hit}} + (1-h) \cdot T_{\text{miss}}

For a multi-level cache:

AMAT=TL1+(1hL1)(TL2+(1hL2)(TL3+(1hL3)Tmem))\text{AMAT} = T_{L1} + (1 - h_{L1}) \cdot (T_{L2} + (1 - h_{L2}) \cdot (T_{L3} + (1 - h_{L3}) \cdot T_{\text{mem}}))

Branch Prediction Accuracy

If the branch is correctly predicted with probability pp and mispredicting costs CC cycles, the expected penalty per branch is (1p)C(1 - p) \cdot C. For typical C=15C = 15 and p=0.97p = 0.97, the expected penalty is 0.450.45 cycles per branch.

Geometric Distribution

The expected number of trials to get a single success when each trial has probability pp is 1/p1/p. Used in modeling rare events (TLB miss after how many memory accesses, etc.).

Independence

Events AA and BB are independent if P(AB)=P(A)P(B)P(A \cap B) = P(A) \cdot P(B). 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 nn.

  • O(f(n))O(f(n)): upper bound. The actual cost grows no faster than f(n)f(n) asymptotically.
  • Ω(f(n))\Omega(f(n)): lower bound.
  • Θ(f(n))\Theta(f(n)): tight bound (both upper and lower).

Common growth rates, in increasing order:

O(1)<O(logn)<O(n)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)O(1) < O(\log n) < O(\sqrt{n}) < O(n) < O(n \log n) < O(n^2) < O(n^3) < O(2^n) < O(n!)

For computer architecture analysis:

  • Cache size scaling: a fully-associative LRU cache has O(n)O(n) tag-comparison cost per access.
  • Set-associative cache: O(ways)O(\text{ways}) comparisons per access (constant w.r.t. cache size).
  • TLB walk: O(levels)O(\text{levels}) memory accesses (4-5 in modern systems).
  • Sorting (relevant to data layout for cache): merge sort is O(nlogn)O(n \log n) in time but O(n)O(n) in extra space; in-place quicksort is also O(nlogn)O(n \log n) on average.

Constants Matter in Practice

Big-O ignores constants, but architecture is largely about constants. An O(n)O(n) algorithm that does cache-friendly sequential access can outperform a "better" O(n/logn)O(n / \log n) 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.

log2(2k)=k\log_2(2^k) = k

A cache with SS sets needs log2S\log_2 S index bits. A 4 KiB page (4096 = 2122^{12} bytes) has 12 offset bits.

The change of base identity: logbx=logax/logab\log_b x = \log_a x / \log_a b.

For asymptotic analysis the base doesn't matter (logs in different bases differ only by a constant), so we usually write O(logn)O(\log n) 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).

Book mode
computer-architectureappendixreference
Was this helpful?