Bits, Bytes, and Memory
A bit is the smallest unit of data — a single 0 or 1. Everything in a computer, from numbers to videos, is stored as sequences of bits.
Why It Matters
Understanding memory layout helps you reason about performance, data sizes, and why certain data structures are faster than others. Cache-friendly code can be 100x faster than cache-hostile code on the same algorithm.
Units
| Unit | Size | Holds |
|---|---|---|
| 1 bit | 0 or 1 | a boolean |
| 1 byte | 8 bits | a character (ASCII) |
| 1 KB | 1,024 bytes | a short email |
| 1 MB | 1,024 KB | a high-res photo |
| 1 GB | 1,024 MB | ~250 songs |
| 1 TB | 1,024 GB | ~500 hours of video |
How Memory Works
RAM is a flat array of bytes, each with an address. When you create a variable, the OS gives your program a chunk of memory.
Address: 0x1000 0x1001 0x1002 0x1003
Content: [0x48] [0x65] [0x6C] [0x6C] = "Hell"
Stack vs Heap
| Property | Stack | Heap |
|---|---|---|
| Allocation | Automatic (function calls) | Manual (malloc/new) |
| Speed | Very fast (pointer bump) | Slower (find free block) |
| Size | Small (usually 1-8 MB) | Large (limited by RAM) |
| Lifetime | Freed when function returns | Freed manually or by GC |
| Layout | Contiguous, LIFO | Fragmented |
Code Example
import sys
# Size of common Python objects in bytes
print(sys.getsizeof(0)) # 28 bytes (int)
print(sys.getsizeof("hello")) # 54 bytes (str)
print(sys.getsizeof([1,2,3])) # 88 bytes (list — just the pointers)
print(sys.getsizeof(True)) # 28 bytes (bool is a subclass of int)
# Arrays are more memory-efficient than lists
from array import array
nums_list = [1.0] * 1000
nums_arr = array('d', [1.0] * 1000)
print(sys.getsizeof(nums_list)) # ~8056 bytes (pointers + objects)
print(sys.getsizeof(nums_arr)) # ~8064 bytes (raw doubles, no pointers per element)Endianness
Multi-byte values can be stored in two orders:
- Big-endian: most significant byte first (
0x12345678stored as12 34 56 78) - Little-endian: least significant byte first (stored as
78 56 34 12)
x86/x64 CPUs use little-endian. Network protocols typically use big-endian (network byte order).
Related
- Binary and Hexadecimal — how values are encoded in bits
- How Computers Execute Code — instructions live in memory too
- Memory Management — how the OS manages memory
- Arrays — contiguous memory = cache-friendly