Brainfuck · Lesson 1 · On the house

Eight commands

The whole language on one still. Lesson 1 is free — on the house.

Lessons · Brainfuck · Lesson 1 of 10 · On the house

Eight commands

The whole language on one still.

Illustration for the Brainfuck course
Lesson 1 is on the house. The rest opens with the paper, the Daily, or a gift.

Eight symbols. That is the entire Brainfuck language. Today you learn what each command does, how to hold the machine model in your head, and why a language this small deserves careful study. By the end you should look at ><+-.,[] and feel instructions — not decoration.

Urban Muller created Brainfuck in 1993 as a deliberately minimal language. It is a microscope, not a shipping toolchain. Memory is a tape of cells; a pointer selects one; programs mutate, move, read, write, and loop. Later idioms are composition of these eight ops.

Map: > moves the data pointer right; < moves left; + adds one to the current cell; - subtracts one; . outputs the cell as a character; , reads a byte into the cell; [ skips to after ] if the cell is zero else enters; ] jumps back to [ if the cell is nonzero else leaves.

Non-command characters are comments — spaces, letters, newlines. Great for teaching; dangerous if a real command glyph sneaks into prose. A stray ] still binds to a [.

Hold two cursors: instruction cursor in the source, data pointer on the tape. Wrong-cell bugs and wrong-idea bugs look alike until you draw state.

C analogy if you know it: unsigned char tape, index i, putchar/getchar, while(tape[i]). Otherwise picture boxes and a finger. Dialects disagree on width, EOF, and left edge; glyphs stay stable. Assume wrapping bytes and pointer at 0 while learning.

Why bother? Constraints teach attention. Copying becomes an algorithm. You feel the cost of ASCII 72 for H. A small set fully understood beats a large set half-remembered.

Pointer discipline means invariants: after each routine, pointer rests on a named home. Document homes. Unbalanced moves inside loops are debt with interest.

Pass the pointer like an API contract between idioms. If copy expects source under pointer, call sites must oblige. Comments are your type system.

When debugging, log pointer index after each idiom on paper. Most mysterious bugs are homes that quietly moved.

Unbalanced loop bodies are the classic stealth bug. Three rights and two lefts per iteration drift one cell right every pass. After ten passes you are lost and blaming increments. Prefer pointer-neutral bodies; if you must drift, comment the invariant. Edge policy: stay non-negative while learning, or park home at cell one so cell zero is a safe left temp.

Movement and mutation do not commute. Right-then-plus is not plus-then-right. When simplifying, rewrite with a trace instead of sliding symbols like algebra. Drill: from cell zero, increment cell three and return home using only moves and one plus. Then decrement cell three and return. If both are easy on paper, navigation is landing.

Read dense moves in groups. Two rights and a left is net one right — unless a plus sits between, in which case order owns you. End every mental block with the sentence: and the pointer is on this index now. Make it as automatic as checking units in physics.

Compare two snippets that look similar: three pluses versus a move then pluses. The first fattens cell zero. The second leaves cell zero alone and fattens a neighbor. Without a tape sketch those blur; with a sketch they never do. Position versus value is this lesson in miniature, and loop bodies will keep mixing the two.

Before you leave, run a mental sim cold: start fresh, execute plus plus right-move plus. Pointer ends on cell one; cell0 holds 2; cell1 holds 1. If that took more than a few seconds, draw it. Speed comes after accuracy. Also remember teaching interpreters almost always zero the tape at start — write programs that rely on that, then document if you port elsewhere.

When you later copy a cell, you will burn a temporary because moving destroys the source unless you dual-write. That future pain is why naming roles on the tape matters now. Counter, source, dest, temp — four words that prevent weeks of confusion. Put them in comments beside your stills.

Deepening ptr (1): pin down input cells, output cells, and pointer home for this idiom. Write those three facts above the still. Trace values 1 and 2 until the postcondition is obvious without an interpreter.

Workbench on ptr (2): every bracket is a while on a named cell. Name the control cell; drive it to zero on purpose. Infinite loops usually mean pointer drift. Re-balance moves before new arithmetic.

Portability for ptr (3): document cell width, EOF, and left-edge assumptions in a header. Fixtures that need wrap fail on unbounded cells. Add a probe still for the dialect you wrote against.

Composition for ptr (4): after the happy path, force failure modes — wrong home, off-by-one, unbalanced moves — and catalog symptoms. Recognition beats restarting from blank paper in larger katas.

Reading ptr (5): rewrite the still with spaces and English labels, then densify again. Accidental plus or bracket characters in labels teach comment hygiene the hard way.

Assessment for ptr (6): predict a tape after a short run, or name which cell a loop tests. If you can teach the still with a five-cell diagram in two minutes, you can pass the lesson cleanly.

Invariant for ptr (7): state precondition and postcondition as cell equalities plus a pointer index. If either side is vague, the idiom is not ready to nest.

Numeric drill on ptr (8): pick three small inputs and fill a table of resulting tapes by hand. Tables catch wrap mistakes a single example hides.

Naming for ptr (9): rename cells to role words in comments — counter, acc, temp, flag, src, dst. Update when roles rotate; stale names lie.

Mistakes: confusing > with +; expecting . to print decimals; ignoring unmatched brackets; thinking non-commands error; treating glyphs as noise.

charset

><+-.,[]
Complete instruction set.

The still is a roster, not a fancy program. Recite each glyph's job left to right until automatic.

Quiz

How many commands does Brainfuck recognize?

Quiz

What does + do?

Quiz

What happens to letters in source?

Quiz

Which pair implements looping?

Quiz

Cell 65 with . prints?

Quiz

Brainfuck is primarily?

Check

Type the eight commands in conventional order (no spaces).

Map acquired. Next: the tape — watch +++ turn zero into three.

Open-book: the answers are on this page. Pass every quiz and check (7) to mark the lesson done. This visit: 0/7.

Eight commands · Brainfuck (free) — The Gold Standard