Cache lines, tags, index, offset
WHY does a cache need to slice the address?
Main memory (DRAM) is huge but slow (~100 ns). The CPU runs at ~0.3 ns/cycle. If every load waited for DRAM, the CPU would idle 99% of the time. So we keep a tiny copy of hot data near the CPU.
But the cache is much smaller than memory, so many memory addresses must share the same cache slot. The hardware needs a fast, wiring-only way (no arithmetic, no search) to:
- Pick which slot a given address maps to → index.
- Confirm that the data sitting in that slot is actually the address we want → tag.
- Select the exact byte(s) inside the fetched block → offset.
Slicing bits is free in hardware (just wires), which is why this scheme wins.
WHAT is a cache line / block?
HOW to derive the three fields from scratch
Assume a byte-addressable memory with -bit addresses, a cache holding sets (rows), each line of size bytes.
Step 1 — Offset bits. Inside a block of bytes we must name each byte. Number of distinct bytes , so we need Why this step? It takes exactly bits to count from to .
Step 2 — Index bits. After removing the offset, the block number is . We must map each block number to one of sets. The hardware uses the low bits of the block number (so consecutive blocks spread across sets): Why this step? bits address rows; low bits are used so adjacent blocks don't collide.
Step 3 — Tag bits. Whatever address bits are left over uniquely identify which of the many blocks that map to this set is currently stored: Why this step? All address bits not used for index/offset must be stored & compared to verify identity.

Associativity (where comes from)
A cache stores total lines. Group them into ways:
- Direct-mapped (-way): each set holds line, so .
- -way set-associative: each set holds lines, so .
- Fully associative: one giant set, ⇒ 0 index bits, everything is tag.
Worked Example 1 — Direct-mapped
Cache: lines, B, direct-mapped, 32-bit addresses.
- Offset bits. Why? 64 bytes per line.
- Sets (direct-mapped) ⇒ index bits. Why? 1 line per set.
- Tag bits. Why? leftover bits.
Address 0xDEADBEEF = 1101 1110 1010 1101 1011 1110 1110 1111.
Carefully peel the low 14 bits (= 8 index + 6 offset). The last 14 bits of the address are
11 1110 1110 1111:
- Offset = low 6 bits =
101111=0x2F(byte 47 in the line). Why? lowest bits. - Index = next 8 bits =
11111011=0xFB= set 251. Why? next bits. (Watch the boundary: the offset eats the bottom 6 bits, so the index is bits 13..6, which read11111011, not10111011.) - Tag = top 18 bits, compared against what's stored in set 251.
Worked Example 2 — 4-way set-associative
Same numbers but 4-way: , , , 32-bit.
- Offset (unchanged — depends only on ).
- Sets ⇒ index bits. Why? fewer sets now.
- Tag bits. Why? index lost 2 bits, tag gained 2 bits.
Worked Example 3 — total bits stored
For the 4-way cache: each line also stores a valid bit and the tag. Bits per line bits. Total = bits of SRAM. Why? overhead (tag+valid) is real silicon cost — bigger lines amortize overhead but waste bandwidth if locality is poor.
Common mistakes (Steel-manned)
Flashcards
Why does a cache fetch whole lines instead of single bytes?
Number of offset bits for line size ?
Number of index bits for sets?
Formula for tag bits with -bit address, sets, -byte lines?
Why use the LOW block-number bits for the index?
Direct-mapped means how many lines per set?
In a -way cache with lines, how many sets?
Going from direct-mapped to -way (same size), tag bits change by?
How many index bits in a fully associative cache?
Does offset size depend on total cache size?
Which field selects the row/set?
Which field confirms identity of the stored block?
For 64-B lines, 256 sets, 32-bit address: tag/index/offset?
For 0xDEADBEEF with 6 offset + 8 index bits, which set?
0x2F (byte 47), index 0xFB = set 251.Recall Feynman: explain to a 12-year-old
Imagine a tiny shelf with a few labeled cubbies near your desk, and a giant warehouse far away. When you grab a book from the warehouse, you grab a whole shrink-wrapped pack of nearby books (that's a cache line) and put it in a cubby. The book's barcode is a long number. The last couple of digits tell you which book inside the pack you want (offset). The middle digits tell you which cubby to put the pack in (index). The first digits are a sticker you slap on the cubby so later you can check "yep, this is the right pack" (tag). Finding a book = go to the cubby (index), check the sticker (tag), grab the right book (offset). Fast, no searching!
Connections
- Cache associativity & replacement policies
- Spatial and temporal locality
- Memory hierarchy and latency numbers
- Virtual vs physical addressing (VIPT/PIPT)
- Cache coherence (MESI)
- TLB and page tables — same "split the address, look up, verify" pattern
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, cache ek chhota magar bahut fast box hai jo CPU ke paas baitha hai, aur main memory bahut badi par slow hai. Har address ko cache ke andar dhoondhne ke liye hardware uss address ko teen hisson mein kaat deta hai: tag, index, offset. Yeh kaatna sirf wires ka kaam hai, koi calculation nahi — isliye super fast.
Samajhne ka aasaan tarika: offset batata hai line ke andar kaun sa byte chahiye, aur yeh sirf
line size par depend karta hai ( bits). Index batata hai cache ki kaun si row
(set) mein dekhna hai ( bits) — aur hum low bits use karte hain taaki pados ke blocks
alag-alag sets mein bikhren aur ek hi set par thrash na ho. Jo bits bach jaate hain woh tag hain,
jo confirm karte hain ki uss slot mein sahi block hi pada hai. Dhyaan rakho: offset 6 bits hai jo
nibble (4-bit hex digit) ke saath align nahi karta, isliye index hex digit ki seedhi boundary par
nahi aata — pehle neeche se offset bits hatao, phir agle index bits lo. 0xDEADBEEF ke liye index
11111011 (0xFB, set 251) hai, na ki 0xBB.
Ek formula yaad rakho: . Associativity badlte ho to total lines fixed rehte hain, bas ways badhao to sets kam (), to index bits kam aur tag bits zyada. Fully associative mein to index zero ho jaata hai — sab kuch tag. Common galti: log mat karo ki offset cache size par depend karta hai — woh sirf line size ka game hai. Aur index ke liye hamesha low block bits, high nahi, warna array thrash kar dega. Bas itna pakka kar lo, exam aur real interviews dono nikal jayenge.