An Interactive Exploration of Computer Architecture & Memory Hierarchy
Computers use base-2 (binary) numbering, where each digit is either 0 or 1.
10102 = 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 1010
Often used as a shorthand for binary, hexadecimal (base-16) simplifies representation of large binary numbers.
Binary: 1101 1010 → Hex: DA
Standards for encoding characters (e.g., letters, symbols) into binary for storage and processing.
'A' → ASCII: 65 → Binary: 01000001
Unit | Size | Typical Use |
---|---|---|
Bit | 1 bit | Basic unit of information (0 or 1) |
Byte | 8 bits | Characters, small integers |
Word | 32/64 bits | Native CPU operations |
Cache Line | 64/128 bytes | Memory transfer unit |
Kilobyte (KB) | 1024 bytes | Small files, program instructions |
Megabyte (MB) | 1024 KB | Medium-sized files, images |
Gigabyte (GB) | 1024 MB | Large files, software applications |
Terabyte (TB) | 1024 GB | Storage for large datasets, backups |
// Binary Operations Examples byte = 0b10101010 // 170 in decimal // Common Operations: AND: 10101010 & 11110000 = 10100000 OR: 10101010 | 00001111 = 10101111 XOR: 10101010 ^ 11110000 = 01011010 NOT: ~10101010 = 01010101 LEFT SHIFT: 10101010 << 2 = 10101000 RIGHT SHIFT: 10101010 >> 2 = 00101010
Operation | Symbol | Use Case | Example |
---|---|---|---|
AND | & | Masking bits, testing bit values | value & mask |
OR | | | Setting bits, combining flags | flags | new_flag |
XOR | ^ | Toggling bits, simple encryption | data ^ key |
NOT | ~ | Inverting bits, creating masks | ~mask |
Left Shift | << | Multiply by powers of 2 | x << 2 (x * 4) |
Right Shift | >> | Divide by powers of 2 | x >> 1 (x / 2) |
// Check if a number is a power of 2 function isPowerOfTwo(x) { return (x & (x - 1)) === 0; } // Count set bits (Hamming weight) function countSetBits(x) { let count = 0; while (x > 0) { count += x & 1; x >>= 1; } return count; } // Swap two variables without temp a ^= b; b ^= a; a ^= b;
Each memory block maps to exactly one cache line.
Simple but can cause collisions
Address: [Tag | Index | Offset]
Any block can go in any cache line.
No collisions but slower search
Address: [Tag | Offset]
Each block maps to a set of cache lines.
Balance between direct and fully associative
Address: [Tag | Index | Offset]
Metric | Definition | Calculation |
---|---|---|
Hit Rate | Fraction of accesses found in cache | Hits / (Hits + Misses) |
Miss Rate | Fraction of accesses not in cache | 1 - Hit Rate |
AMAT | Average Memory Access Time | Hit Time + (Miss Rate × Miss Penalty) |
Virtual Address Space |--------------------| | Page Table | |--------------------| | Translation | →→→ TLB Cache →→→ Physical Memory |--------------------| | Page Mapping | |--------------------|
Multi-level page tables to save space
Virtual Address: [PD Index | PT Index | Offset]
Hash function maps virtual to physical
Good for large sparse address spaces
One entry per physical page
Space efficient but slower lookups
Algorithm | Description | Advantage | Disadvantage |
---|---|---|---|
LRU | Least Recently Used | Optimal for temporal locality | Expensive to implement |
Clock | Second-chance algorithm | Efficient approximation of LRU | Not truly LRU |
Working Set | Track active pages | Prevents thrashing | Complex implementation |
FIFO | First In First Out | Simple to implement | Poor performance (Belady's anomaly) |
Strategy | Advantages | Disadvantages | Use Case |
---|---|---|---|
First Fit | Simple, fast allocation | Can lead to fragmentation | General purpose allocators |
Best Fit | Minimizes wasted space | Slower allocation time | Memory-constrained systems |
Buddy System | Fast, minimal fragmentation | Some internal fragmentation | Kernel memory allocators |
Slab Allocation | Fast for fixed-size objects | Not flexible for variable sizes | Object caches |
Traverses and marks reachable objects, then sweeps unreachable ones
• Handles cyclic references
• Can cause pauses
• Used in JavaScript, Java
Counts references to each object, frees when count reaches zero
• Immediate reclamation
• Can't handle cycles
• Used in Python, PHP
Divides heap into generations, collects younger generations more often
• Based on weak generational hypothesis
• Reduces pause times
• Used in modern JVMs
MMU Functions: 1. Address Translation (via page tables) 2. Memory Protection (page permissions) 3. Cache Control (cacheability attributes) 4. TLB Management (translation lookaside buffer)