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

UnitSizeHolds
1 bit0 or 1a boolean
1 byte8 bitsa character (ASCII)
1 KB1,024 bytesa short email
1 MB1,024 KBa high-res photo
1 GB1,024 MB~250 songs
1 TB1,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

PropertyStackHeap
AllocationAutomatic (function calls)Manual (malloc/new)
SpeedVery fast (pointer bump)Slower (find free block)
SizeSmall (usually 1-8 MB)Large (limited by RAM)
LifetimeFreed when function returnsFreed manually or by GC
LayoutContiguous, LIFOFragmented

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 (0x12345678 stored as 12 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).