Intuition The ONE core idea
A cache is a tiny fast notebook that holds copies of pages from a giant slow book, and every piece of notation in this topic exists to answer a single question: "given a memory address, where in the notebook is that page allowed to sit, and is the right page actually there?" Master how an address is chopped into three fields, and every formula about direct-mapped, set-associative, and fully-associative caches falls out mechanically.
Before you can read the parent note, you must own eleven small ideas. We build them one at a time — each one earns the next. Nothing is assumed.
A bit is a single yes/no switch: it is either 0 or 1 . That's the whole idea. Nothing in a computer stores anything except long rows of these switches.
Why does hardware love powers of two? Because with k switches side by side you can make exactly 2 k different patterns — and every pattern is used, none wasted.
Intuition Read the picture
With 1 bit you get 2 patterns (0 , 1 ). With 2 bits you get 4 (00 , 01 , 10 , 11 ). With 3 bits, 8 . The count doubles every time you add a switch. So "k bits" and "2 k things I can name" are two ways of saying the same fact.
This is the single arithmetic move the whole topic runs on:
Why do we need a new symbol instead of ordinary multiplication? Because doubling and un-doubling are the exact question the address split asks: "I have 256 boxes — how many switches address them?" Multiplication gives you the count forward (2 × 2 × … ); log 2 runs it backward .
log 2 n in plain words
log 2 n = the exponent you must raise 2 to, to land on n .
log 2 8 = 3 because 2 3 = 8 . log 2 64 = 6 because 2 6 = 64 . log 2 256 = 8 because 2 8 = 256 .
Mnemonic Count the doublings
2 → 4 → 8 → 16 → 32 → 64 . That is five arrows, so log 2 64 = 6 ? No — count the numbers after the first : from 2 we doubled 5 times to hit 64 , and log 2 2 = 1 , so log 2 64 = 1 + 5 = 6 . Safest: just find the exponent making 2 x = n .
m
Main memory is a long shelf of bytes (a byte = 8 bits, the smallest thing memory hands out). Each byte has a number called its address . The address is itself a row of bits; ==m == is how many bits that row has.
If m = 32 , there are 2 32 distinct addresses, i.e. 2 32 bytes of addressable memory. The parent note's "2 32 bytes" is literally "m = 32 " spoken out loud.
Intuition Byte-addressable
"Byte-addressable" means every address points at one byte, not one bit and not one block. Keep this straight — it decides how many offset bits we need in step 6.
Memory is not copied one byte at a time — that would waste the trip. It moves in fixed chunks .
Definition Block / line, size
B
A block (also called a line ) is a run of ==B == consecutive bytes that always travels together between memory and cache. B is always a power of two (e.g. 64 ).
Why chunks? Because programs tend to use neighbours soon after each other (this is spatial locality ) — grabbing the whole neighbourhood at once pays off.
Intuition Read the picture
The long strip is memory. It is cut into blocks of B bytes. Block number k contains bytes k B through k B + B − 1 . To find a specific byte you need two facts: which block (its number k ) and which byte inside it (the offset). That two-part question is the seed of the whole address split.
Take any byte address and ask the two questions above.
Intuition It's just cutting the bit-row
Because B is a power of two, dividing by B is the same as chopping off the lowest log 2 B bits . Those chopped bits ARE the offset; what remains ARE the block number. No arithmetic, just a scissor cut in the row of bits.
Why these two? Because "which block" and "which byte inside" are exactly integer division and remainder .
⌊ x ⌋ (floor ) = round down to the nearest whole number. ⌊ 7/4 ⌋ = 1 .
a mod n (mod ) = the remainder after dividing a by n . 7 mod 4 = 3 .
Together they answer: "7 split into groups of 4 = 1 full group with 3 left over."
Intuition Why mod is the "which set" machine
k mod S always lands in { 0 , 1 , … , S − 1 } — exactly S possible answers. So mod is the natural tool for "sort this block into one of S bins" , which is precisely what choosing a cache set is.
S , E , N
The cache is organised as a grid:
==S == = number of sets (rows). Choosing a set is done with the index bits.
==E == = number of ways = blocks per set (columns). This is the associativity .
==N = S ⋅ E == = total number of block slots the cache holds.
Every block slot in the cache sits at a (set, way) coordinate. The index picks the row; then all E slots in that row are searched at once. Three famous shapes are just extreme grids:
E = 1 : one column → direct-mapped (tallest grid).
S = 1 : one row → fully associative (widest grid).
in between → n -way set associative .
Now we name the widths (bit counts) of the three fields. Each is a small log 2 .
Intuition What each field
does
offset b — picks the byte inside the block (needs log 2 B bits, one per byte-choice).
index s — picks the set/row (needs log 2 S bits, one per set-choice).
tag t — everything left over; it is the block's ID card, stored in the slot so we can later confirm "yes, this block is here, not some other block that also maps to this set."
Common mistake "Fully associative has no tag because
s = 0 ."
Why it feels right: no index, so it seems nothing is left. Truth: t = m − 0 − b is the largest tag of all. With no index to pre-sort blocks, the tag must carry the entire block number.
When the machine powers on, every slot holds garbage. We need one switch per slot to say "ignore me until I've been loaded."
A valid bit is a single bit attached to each cache slot: 1 = "this slot holds a real block, its tag is meaningful," 0 = "empty/garbage, never a hit." A hit requires valid = 1 AND matching tag.
Without it, a random leftover tag could accidentally "match" and hand you wrong data. It is the AND-guard on every lookup.
Definition Hit / miss / comparator
Hit = the block you asked for is already in the cache (valid + tag match). Fast.
Miss = it isn't; you must fetch it from slow memory.
A comparator is a piece of hardware that checks "are these two tags equal?" and answers yes/no in one cycle. An E -way cache needs E comparators (one per slot in a set), all firing together.
This is why fully associative (E = N ) is expensive: N comparators screaming on every access. It leads straight into average access time reasoning and the 3 C's miss model you'll meet later.
Putting it together: a block with number k is sent to set
set of block k = k mod S .
low bits of the block number
k mod S (with S a power of two) keeps only the lowest s bits of the block number. Consecutive blocks k , k + 1 , k + 2 , … then land in different sets — spreading a burst of neighbouring accesses across the cache instead of piling them in one row. That cooperation with locality is deliberate.
Once E > 1 , two blocks sharing a set can both stay — but then you must choose whom to evict on a later miss, which is the job of replacement policies . Direct-mapped (E = 1 ) never chooses: one legal slot, no policy needed.
Cache organization the topic
You are ready for the parent note when you can answer each without peeking.
How many distinct patterns do k bits make? ::: 2 k .
What does log 2 n ask? ::: "2 to what power equals n ?" — the number of doublings.
What is log 2 64 and why? ::: 6 , because 2 6 = 64 .
What does m stand for and what is 2 m ? ::: address width in bits; 2 m = number of addressable bytes.
Why is memory moved in blocks of B bytes, not single bytes? ::: to exploit spatial locality — neighbours get used soon.
Given a byte address, how do you get its block number? ::: ⌊ address / B ⌋ (chop off the low log 2 B bits).
What does a mod n compute and why is it perfect for choosing a set? ::: the remainder; it always lands in { 0 , … , n − 1 } , one bin per set.
Define S , E , N and their relationship. ::: sets, ways (blocks per set), total slots; N = S ⋅ E .
Give the three field widths. ::: b = log 2 B , s = log 2 S , t = m − s − b , with t + s + b = m .
Why does fully associative have the largest tag? ::: s = 0 , so t = m − b ; the tag must hold the whole block number.
What two conditions make a hit? ::: valid bit = 1 AND stored tag equals address tag.
How many comparators does an E -way cache need? ::: exactly E .
Which set does block k map to, and which bits does that use? ::: k mod S , using the lowest s bits of the block number.