Brainfuck Easy · Lesson 1 · On the house

Cell wrapping

Bytes live on a ring of 256. Lesson 1 is free — on the house.

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

Cell wrapping

Bytes live on a ring of 256.

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

Classic Brainfuck cells are bytes that wrap mod 256. 255+1 becomes 0; 0-1 becomes 255. Today you treat wrap as modular arithmetic you can prove — not as a mystery glitch.

Loops that expect to hit zero can miss if you wrap the wrong way. Prefer [-] clear (minus until zero) over adding until overflow unless you intentionally exploit the ring.

Probe your interpreter: build 255, add one, emit or inspect. Document width if you share code. Sixteen-bit and unbounded cells change which tricks are legal.

Wrap can implement sentinels cleverly; it can also turn a near-zero counter into 255 and spin forever. Trace near boundaries every time.

Byte cells wrap mod 256. Plus on 255 yields 0; minus on 0 yields 255. Loops that expect to hit zero can miss if you wrap the wrong way. Prefer minus-until-zero to clear. Use wrap intentionally only when you can prove the ring arithmetic.

Test wrap on your interpreter with a tiny program. Document cell width if you share code. Sixteen-bit and unbounded cells change which tricks are legal. Never assume wrap mid-algorithm without a comment.

Wrap can implement certain decrements and sentinels cleverly; it can also turn a near-zero counter into 255 and spin a loop. Trace near the boundary every time.

Constants are programs. Seven is seven pluses on a known zero. Mid-program, forcing seven usually means clear then seven pluses — clearing arrives in Easy. Group pluses in fives so eyes can count. Losing count is how Hello becomes Helln. Watch plus characters in comments; they execute.

Wrap is modular arithmetic on a 256-ring for classic bytes. Clearing by adding until overflow is a party trick; the default clear is a minus loop. Near zero, a chain of pluses and minuses can dive through zero into 255 and surprise a later loop test. Be deliberate around the cliff.

ASCII targets: 65 A, 97 a, 48 digit zero, 32 space, 10 newline. Building sixty-five pluses is legal; loops compress later. Crave compression after you can land exact values. Exactness beats elegance while letters are wrong by one.

Deepening wrap (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 wrap (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 wrap (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 wrap (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 wrap (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 wrap (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 wrap (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 wrap (8): pick three small inputs and fill a table of resulting tapes by hand. Tables catch wrap mistakes a single example hides.

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

Bracket hygiene for wrap (10): indent bodies even though whitespace is ignored. Match pairs with a stack while reading dense one-liners.

Emit-as-debug for wrap (11): park known letter emits at phase boundaries while building; remove markers when fixtures pass.

Stop condition for wrap (12): say what makes the loop end. If you cannot, you do not control it. Zero is native false — design for it.

Caller contract for wrap (13): document clobbered cells and final pointer home like an ABI. Ignoring ABI creates heisenbugs across katas.

Minimal still for wrap (14): delete commands until the teaching point breaks, then put one back. Remember the minimal still, not the longest golf.

Edge cases for wrap (15): try zero inputs, try one, try a value near 255 on byte cells. Boundaries reveal wrap and loop mistakes early.

Refactor note for wrap (16): after it works, rename temps and rebalance moves for readability before you nest this idiom inside a larger program.

Teach-back for wrap (17): explain the still to an empty chair with only a five-cell diagram. Hesitation marks the exact gap to restudy.

History of practice for wrap (20): the first time you get this wrong, write the mistaken tape beside the correct tape. The diff is the lesson. Keep a scrap log of diffs for this topic.

Scaling wrap (21): once the tiny example works, scale counters to 5 and 7 before you leap to ASCII sizes. Medium numbers catch off-by-ones without drowning you in pluses.

Interleave for wrap (22): alternate one minute of tracing with one minute of typing the still from memory. Retrieval practice beats passive rereading for command sequences.

Negative space for wrap (23): list what the idiom does not do — no implicit moves, no implicit clears, no decimal printing. Negatives prevent false expectations.

Fixture seed for wrap (24): invent one expected output string or final cell vector. Even a private fixture makes later golf honest instead of theatrical.

clear

[-]
Clear current cell by minus-loop (safe default).

[-] while nonzero decrement — ends at 0, pointer stays. Safer than overflow-to-zero.

wrapplus

+
After 255, + wraps to 0 on byte cells.

Unary + on a cell already at 255 lands on 0 if width is 8 bits.

Quiz

255+1 on an 8-bit cell?

Quiz

Safe default clear?

Quiz

0-1 on bytes usually?

Quiz

Why document cell width?

Quiz

Risk of accidental wrap in a counter?

Quiz

Unbounded integer cells wrap at 256?

Check

Type the clear idiom.

Check

Type a single + (wrap demo fragment).

Next: nested loops — multiplying structure.

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

Cell wrapping · Brainfuck Easy (free) — The Gold Standard