Skip to main content

Introduction

Before diving into data structures, it helps to understand how computers store data at a low level. Computers process data in binary form, represented by bits (0s and 1s). These bits are grouped into bytes, and bytes represent various types of data such as integers, characters, and more complex structures. As a developer, you work with variables in your code. These variables are stored in the computer’s memory so they can be accessed and manipulated during program execution — this is where RAM comes in.

What is RAM?

RAM stands for Random Access Memory — a type of computer memory used to store data that is actively being used or processed. RAM is volatile memory, meaning it loses its contents when the computer is turned off.
1 byte = 8 bits. A bit is a 0 or 1. RAM capacity is measured in bytes (KB, MB, GB).
Modern computers typically have at least 8 GB of RAM. More RAM means more data can be processed simultaneously.
RAM SizeBytesIntegers (4 bytes each)
8 GB8,589,934,592~2.1 billion
16 GB17,179,869,184~4.3 billion

Components of RAM

RAM can be thought of as a contiguous block of data where each location has two components:
  • Value — the actual data being stored (e.g., an integer, a character).
  • Address — the location where the value is stored in memory, used to access the value.
What is a contiguous block of data? Data is stored in a continuous sequence of memory addresses with no gaps. If an array starts at address 1000 and each element takes 4 bytes, the second element is at 1004, the third at 1008, and so on. This enables fast O(1) index access by computing address = base + (index × element_size).

Binary Representation

Computers use binary to store and process all data. Each digit in a binary number is a bit. Multiple bits are grouped to form larger data types:
UnitBitsExample
Bit11
Byte800000101
Word1600000000 00000101
Double Word3200000000 00000000 00000000 00000101
Binary numbers are based on powers of 2. Each position represents a power of 2, starting from 2⁰ on the right.
Position:  7    6    5    4    3    2    1    0
================================================
Power:     2⁷   2⁶   2⁵   2⁴   2³   2²   2¹   2⁰
Value:     128  64   32   16   8    4    2    1
To convert binary to decimal: Add up the values where the bit is 1.
For example, the decimal number 5 in binary:
  • 8-bit: 00000101
  • 32-bit: 00000000 00000000 00000000 00000101
Binary representation of the first 10 decimal numbers:
Decimal8-bit16-bit32-bit
10000000100000000 00000001... 00000001
20000001000000000 00000010... 00000010
30000001100000000 00000011... 00000011
40000010000000000 00000100... 00000100
50000010100000000 00000101... 00000101
80000100000000000 00001000... 00001000
100000101000000000 00001010... 00001010

How Data is Stored in RAM

Consider the array [1, 2, 3]. Each integer is converted to binary and stored in contiguous memory addresses.
Address: 100  │ 00000001  (Integer: 1)
Address: 104  │ 00000010  (Integer: 2)
Address: 108  │ 00000011  (Integer: 3)
Why does the address increment by 4? Each integer takes 4 bytes (32 bits) of memory. Since each memory address points to 1 byte, you need 4 consecutive addresses per integer.Address formula: Element address = base_address + (index × element_size)
  • Array[0] = 100 + (0 × 4) = 100
  • Array[1] = 100 + (1 × 4) = 104
  • Array[2] = 100 + (2 × 4) = 108
Elements in the array are stored continuously and in order. You cannot choose which memory location the data is placed in — the OS and runtime handle that.

Build docs developers (and LLMs) love