🧮 Foundations — Before C
Bits & Binary: how machines count
▶ Open the interactive lesson — free, no signupEvery photo, song, and game on your phone is, deep down, nothing but billions of tiny on/off switches. Learn to read those switches and you can count to any number using just two symbols — and you'll finally know why computer sizes always come as 64, 128, or 256, never a nice round 100.
Deep down, a computer is just billions of tiny switches. Each switch is either off or on — we write that as 0 or 1 and call it a bit (a binary digit). That's the entire alphabet of the CPU — the chip that does your computer's actual thinking. Everything you'll ever program — photos, games, this very page — is built out of those two symbols.
Why only two? Electrically, "is there voltage or not?" is easy and reliable to detect. Ten different voltage levels (for decimal) would be fragile and slow. Two states = cheap, fast, and nearly error-proof.
Counting with two fingers
You count in base 10 because you have ten fingers: each digit position is worth 10× the one to its right (…1000, 100, 10, 1). Binary works exactly the same way, except each position is worth 2× the one to its right: …8, 4, 2, 1.
So binary 1011 means: 1×8 + 0×4 + 1×2 + 1×1 = 11.
Eight bits make a byte — the standard "unit" of memory. Play with one below: can you make 42? What's the biggest number a byte can hold?
▶ This spot has an interactive bits widget — open the interactive lesson to play with it.
🧠 Checkpoint: What is binary 1101 in decimal?
- 11
- 13
- 15
- 9
Show answer
13 — 1101 = 8 + 4 + 0 + 1 = 13. Read right-to-left: ones place (1), twos place (0), fours place (1), eights place (1).
Powers of two are everywhere
Every bit you add doubles how many values you can represent. That's why the same "magic numbers" keep showing up in computing:
| bits | distinct values | you know it as… |
|---|---|---|
| 1 | 2 | a boolean: true / false |
| 8 | 256 | a byte — char in C, values 0…255 |
| 16 | 65,536 | short — ports, Unicode's first plane |
| 32 | ~4.3 billion | int on most machines, IPv4 addresses |
| 64 | ~1.8 × 1019 | long long, pointers on your PC |
Memorize the small powers of two — 1, 2, 4, 8, 16, 32, 64, 128, 256 — they'll be your times tables for the rest of this course.
🧠 Checkpoint: A byte holds 8 bits. How many different values can it represent?
- 8
- 128
- 255
- 256
Show answer
256 — 2⁸ = 256 different values — from 0 up to 255. A classic off-by-one trap: 255 is the biggest value, but there are 256 values including zero.
Binary in real C code
C lets you write binary literals directly with the 0b prefix (official since C23, supported by GCC and Clang for years):
#include <stdio.h>
int main(void) {
int answer = 0b101010; /* binary literal: 42 */
int mask = 0b11110000; /* the top 4 bits of a byte */
printf("answer = %d\n", answer);
printf("mask = %d\n", mask);
return 0;
}$ gcc binary.c -o binary && ./binary answer = 42 mask = 240
🧠 Checkpoint: Each extra bit __________ the number of representable values.
- adds one to
- doubles
- squares
- adds two to
Show answer
doubles — n bits give 2ⁿ values, so one more bit gives 2ⁿ⁺¹ — exactly double. This doubling is the deep reason computers love powers of two.
Next up: writing 11111111 gets old fast. Programmers have a beautiful shorthand for binary — hexadecimal.