Direct Mapping in Cache Memory: Address Bits, Cache-Line Mapping and a Hit-Ratio Worked Example
Learn how a direct-mapped cache chooses one line, splits a byte address into tag, index and offset, and creates conflict misses in a worked trace.
KnowledgeGate Team
Exam prep & CS education

The formula line = block number mod number of cache lines looks easy. Mistakes begin when a question gives you a byte address and asks for its tag, line, offset, hit or miss. A direct-mapped cache derives the tag, line, and offset from the address, then uses the mapping to identify hits, misses, and conflict replacements.
Direct mapping in cache memory: one block, one possible line
The cache sits between the processor and main memory. Main memory is divided into equal blocks, while the cache holds block-sized cache lines. Each line stores data, a tag that identifies the resident memory block, and a valid bit showing whether its contents can be used. A byte offset selects a byte within that block. A matching valid entry is a hit; otherwise, it is a miss.
For a cache with L lines, block j can occupy only line j mod L.
Main-memory blocks | Only possible cache line |
|---|---|
0, 8, 16, 24 | 0 |
5, 13, 21, 29 | 5 |
Thus, loading block 13 after block 5 replaces block 5, even if the other seven lines are unused. There is no replacement-policy choice because the mapping fixes the victim. Cache Memory: Mapping and Hit Ratio gives the broader comparison of mapping organisations and hit-ratio calculations. Cache Mapping Techniques and Memory Units holds one address constant across direct, fully associative and 4-way placement; the narrower task here is one direct-mapped cache's address split and conflict trace.

Direct-mapped cache address format: derive tag, index and offset
Use a 32-bit byte-addressable memory, a cache data capacity of 16 KiB = 16,384 = 2^14 bytes, and a block size of 64 = 2^6 bytes. Unless a question explicitly includes metadata, cache capacity means data bytes.
Cache lines =
16,384 / 64 = 256 = 2^8.Byte-offset bits =
log2(64) = 6.Line-index bits =
log2(256) = 8.Tag bits =
32 - 8 - 6 = 18.
The address format is therefore 18-bit tag | 8-bit line index | 6-bit byte offset.
The reusable formulas are lines = cache data bytes / block bytes, offset bits = log2(block bytes), index bits = log2(lines), and tag bits = address bits - index bits - offset bits. Direct bit slicing assumes power-of-two sizes. For word-addressable memory, derive the offset from the addressed unit instead of silently assuming byte addressing.
Worked direct-mapping example: map 0x12345678 step by step
Memory Organisation and Performance traces the same address through a 32 KiB cache and carries it into AMAT. Here, the 16 KiB cache shifts one address bit from the index into the tag, and the later line-89 trace isolates direct-mapping conflicts. Split the address according to the derived field widths:
0x12345678 = 000100100011010001 | 01011001 | 111000₂
This gives tag 000100100011010001₂ = 0x48D1 = 18,641, line index 01011001₂ = 0x59 = 89, and byte offset 111000₂ = 0x38 = 56.
Now cross-check with arithmetic:
Block number =
floor(0x12345678 / 64) = 0x48D159 = 4,772,185.Line =
4,772,185 mod 256 = 89.Tag =
floor(4,772,185 / 256) = 18,641.The block spans
0x12345640to0x1234567F, and offset 56 reaches0x12345678.
Reconstruction gives a final check: (0x48D1 << 14) + (0x59 << 6) + 0x38 = 0x12345678.
The lookup inspects line 89. It hits only when valid = 1 and the stored tag is 0x48D1. Otherwise, the cache fetches block 0x12345640-0x1234567F, writes tag 0x48D1, sets the valid bit, and returns byte 56.

Direct mapping hit and conflict-miss trace on the same line
Start with an empty cache and trace four byte addresses. The state column shows the tag left in line 89 after each access.
Access | Block range | Tag | Line | Offset | Result | Line-89 state after access |
|---|---|---|---|---|---|---|
|
|
| 89 | 56 | Compulsory miss | Valid, tag |
|
|
| 89 | 60 | Hit | Valid, tag |
|
|
| 89 | 56 | Conflict miss | Valid, tag |
|
|
| 89 | 56 | Conflict miss | Valid, tag |
The second address is in the block already loaded, so it hits. The third address is exactly 0x4000 = 16 KiB higher than the first. It keeps the same line and offset but changes the tag, forcing a replacement. Returning to the first address forces another replacement.
The totals are 1 hit and 3 misses in 4 accesses. Hit ratio = 1/4 = 25%; miss ratio = 3/4 = 75%. In this cache, addresses one cache-data-capacity apart, with the same position inside their blocks, map to the same line.
What direct mapping buys and what it gives up
The eight index bits select exactly one of 256 lines. Hardware compares one stored 18-bit tag, then the six offset bits select one of 64 bytes. It does not search every line or maintain LRU or FIFO replacement state.
That simple path has a cost. Blocks with tags 0x48D1 and 0x48D2 cannot coexist because both are forced into line 89, even when some other lines are empty. These are conflict misses, not proof that the entire cache lacks capacity.
Direct mapping has one candidate line and one tag comparison. An E-way set-associative cache has E candidates inside one indexed set. A fully associative cache may place a block in any line. The best organisation depends on workload and hardware constraints.
Direct-mapping traps that change the numerical answer
Trap | Why it happens | Wrong consequence | Correction |
|---|---|---|---|
Using cache size for offset bits | Cache capacity and block size are mixed up | Far too many offset bits | Compute offset from block size only |
Treating 16 KiB as 16,000 bytes | The binary meaning of KiB is ignored | Wrong line count and field widths | Use |
Applying modulo to the byte address | The block step is skipped | Bytes of one block appear to use different lines | Find the block number first |
Subtracting tag bits too early | The line count is not yet known | Index and tag widths are guessed | Compute lines, offset, index, then tag |
Word-addressable memory does not justify byte-offset bits unless the specification supports them. Likewise, if cache size includes tags and status bits, do not treat the full figure as data capacity.
For this data-only cache, tag-plus-valid storage is 256 x (18 + 1) = 4,864 bits = 608 bytes. If one dirty bit per line is explicitly added, it becomes 256 x 20 = 5,120 bits = 640 bytes. Dirty-bit accounting does not change the placement rule.
How exam questions test direct mapping
Common problems ask you to derive field widths, map a hexadecimal or decimal address, decide hit or miss from valid-tag contents, trace conflicts, compute a hit ratio or tag-store overhead, or reconstruct an address.
Use a four-pass method:
Normalise every size to a power of two.
Compute the block size and number of lines.
Draw the address fields before inserting an address.
Keep a small per-line tag table while tracing accesses.
Reconstruct the original address after every split. No paper-specific assumption is needed to solve the underlying cache calculation.
Direct mapping in cache memory: the short version and next step
16 KiB / 64 bytes gives 256 lines. A 64-byte block needs six offset bits, 256 lines need eight index bits, and a 32-bit address leaves 18 tag bits. Therefore, 0x12345678 maps to tag 0x48D1, line 89, offset 56. A hit needs both a set valid bit and a matching tag. As a self-check, 0x12349678 maps to tag 0x48D2, line 89, offset 56, so the two blocks conflict.
For a structured subject-wise route, use GATE Guidance by Sanchit Sir. The GATE CS Exam Preparation Courses and Test Series page lets you compare available resources.
Keep learning
Cache Memory Organization in COA: Mapping, Hit Ratio and Worked Examples
Learn cache organization through one 64 KB memory and 1 KB cache example, carried from address fields and mapping to AMAT, writes, replacement and directory size.
Cache memory: mapping techniques and hit ratio, with the exam angle
Cache memory explained: the memory hierarchy, locality, direct, associative and set-associative mapping, plus a worked hit-ratio and access-time example.
Cache Mapping Techniques and Memory Units: Direct, Associative and Set-Associative Worked Example
Use one address to learn memory-unit conversion, cache organisation and the exact tag, index and offset fields for all three mapping techniques.
Associative Mapping in Cache Memory: Address Fields, Worked Example and Exam Traps
Learn why fully associative cache addresses have no index field. Derive the tag, follow an exact LRU trace, compare mapping methods and avoid common exam mistakes.





