Exercises — Cache line size and tags
This page is your training ground for the parent topic. Every problem below is fully solved inside a collapsible callout — try it first, then reveal. The levels climb from recognising the fields to designing a cache from scratch.
Before we start, one figure fixes the vocabulary so no symbol is ever unexplained.

Level 1 — Recognition
Exercise 1.1
A cache uses 128-byte lines. How many offset bits does each address need?
Recall Solution 1.1
Offset must name every byte inside one line. There are byte-positions. Why ? 128 distinct positions need exactly enough bits to count through , and . So 7 bits.
Exercise 1.2
A direct-mapped cache has lines. How many index bits?
Recall Solution 1.2
Direct-mapped means , so number of sets = number of lines = . The index picks one slot out of . 11 bits.
Exercise 1.3
Given the field widths , , , what is the total address width ?
Recall Solution 1.3
The three fields are the whole address, edge to edge — no bit is left over or shared. 32-bit address space.
Level 2 — Application
Exercise 2.1
A 16 KiB direct-mapped cache uses 32-byte lines on a 32-bit machine. Find , , .
Recall Solution 2.1
Step 1 — offset. . (names each byte in the line)
Step 2 — number of lines. B. Lines .
Step 3 — index. , so . (names each slot)
Step 4 — tag. .
Answer: [18-bit Tag | 9-bit Index | 5-bit Offset].
Exercise 2.2
Same cache as 2.1. The CPU requests address 0x0000_2C64. Split it into Tag / Index / Offset.
Recall Solution 2.2
First, 0x2C64 = (decimal).
In 32-bit binary:
0000 0000 0000 0000 0010 1100 0110 0100
Split from the top as [18 tag | 9 index | 5 offset]:
- Offset = low 5 bits =
0 0100= 4. - Index = next 9 bits =
011 0011shifted... let's compute directly. Drop the offset: is the line number. Index . - Tag = high bits .
Answer: Tag , Index , Offset . Check: ✓ (note , so tag is multiplied by ).
Exercise 2.3
For the cache in 2.1, how many bits of tag storage does the whole cache use (tag + valid bit, no dirty bit)?
Recall Solution 2.3
Per line: tag bits, valid bit bits. There are lines.
Level 3 — Analysis
Exercise 3.1
Two engineers argue about a fixed 32 KiB, direct-mapped, 32-bit cache. Engineer A doubles the line size from 64 B to 128 B. What happens to (a) index bits, (b) tag bits, (c) total tag-storage bytes? Explain each direction.
Recall Solution 3.1
Cache size B stays fixed. , .
Case 64 B: . Lines . . Tag storage (tag + valid) bits B.
Case 128 B: . Lines . . Tag storage bits B.
(a) Index: — decreases by 1, because doubling line size halves the number of lines. (b) Tag: — unchanged here. We lost 1 offset bit but also lost 1 index bit; they cancel (, and stayed at 15). (c) Tag storage: B — halved, because there are half as many lines, each carrying the same 18 metadata bits.
Exercise 3.2
A cache designer keeps line size B and address width , but grows the cache from 16 KiB to 64 KiB (both direct-mapped). Show that tag bits shrink and by how much.
Recall Solution 3.2
throughout.
16 KiB: lines . . 64 KiB: lines . .
Growing the cache adds index bits, which steals 2 bits from the tag: . Intuition: a bigger cache has more slots, so more of the address is spent saying which slot — leaving fewer bits to distinguish blocks that land in the same slot.
Level 4 — Synthesis
Exercise 4.1
Design task. A 64-bit machine needs a 4-way set-associative cache, total size KiB, line size B. Compute sets, , , , and the total metadata (tag + valid + dirty) in KiB.
Recall Solution 4.1
Step 1 — offset. . Step 2 — total lines. lines. Step 3 — sets. Set-associative packs lines per set: So (the index now names a set, not a single line). Step 4 — tag. bits. Step 5 — metadata. Each of the lines carries tag (48) + valid (1) + dirty (1) bits. Answer: 1024 sets, , , ; metadata KiB.
Exercise 4.2
Same as 4.1 but make it direct-mapped (A=1), everything else identical. Which changes — , , or — and why does the tag grow?
Recall Solution 4.2
(line size unchanged). Now sets lines . bits.
Compared to 4.1: index rose (+2) and tag fell (−2)... wait — the tag shrank, not grew. With the same , direct-mapped has more sets ( vs ), so it needs more index bits and fewer tag bits.
Lesson: raising associativity (fewer sets) grows the tag; lowering it shrinks the tag. The set-associative version (4.1, ) has the larger tag.
Level 5 — Mastery
Exercise 5.1
Reverse-engineering. You are told an address splits as [Tag | Index | Offset] with the lookup circuit using index bits and offset bits on a 32-bit machine, and the cache is 2-way set-associative. Recover , number of sets, total cache size , and .
Recall Solution 5.1
Line size: B. Sets: sets . Cache size: B KiB. Tag: bits. Sanity check via formula: ✓. Answer: B, 128 sets, KiB, .
Exercise 5.2
Capstone trade-off. Two designs, both 32-bit, 16 KiB, direct-mapped:
- Design P: B.
- Design Q: B. Compute the metadata overhead fraction (tag + valid + dirty per line, over data bytes) for each, and state which wins on overhead and what each sacrifices.
Recall Solution 5.2
B, , .
Design P (): . Lines . . Metadata/line bits B. Data/line B. Overhead fraction .
Design Q (): . Lines . . Metadata/line bits B. Data/line B. Overhead fraction .
Winner on overhead: Design Q — big lines amortise the fixed 20-bit tag block over far more data (2.0% vs 15.6%). The sacrifice: Q has only 128 lines vs P's 1024, so Q suffers more conflict misses and a higher miss penalty (128 B transfers). P wastes chip area on metadata but keeps lots of distinct slots. This is exactly the spatial-locality trade-off that lands most real CPUs at 64 B — the middle ground.

Recall One-line field recovery cheat card
From widths: ; sets ; ; . To split an address value: offset ; line# ; index ; tag .
Connections
- Cache line size and tags — the parent theory these drills exercise.
- 5.4.02-Direct-mapped-caches — L1–L3 assume .
- 5.4.03-Set-associative-caches — L4–L5 introduce sets and the step.
- 5.4.05-Cache-miss-types — the conflict-miss cost behind the L5 trade-off.
- 5.4.08-Write-policies — where the dirty bit in the metadata counts comes from.
- 3.1.04-Spatial-locality — why bigger lines help at all.
#flashcards/hardware