LearnCode patterns
Code patterns
Short pieces of brainfuck that each do one job: move a number, copy it, decide, calculate, print. Run each one, step through it, change its values, copy it.
One pattern at work
The program below copies the number in the first cell into the second cell and keeps it in the first. Press Play, drag the step slider forwards and back, or change x:
The figure needs JavaScript.
Notice that the number never travelled in one piece. The first loop emptied x and gave each unit to two cells at once, the copy and the third cell. The second loop poured the third cell back into x. A cell can only be read by emptying it, so a copy needs a helper that starts and ends at 0. A helper like that is a scratch cell.
This program is a code pattern: a short piece of brainfuck that does one job, written so that every cell starts and ends in a known state. Every pattern here has a figure like this one, the program to copy, what it costs, and other forms of the same job. The marks you saw are the same everywhere: the pointer is the cell with the heavy edge and the triangle, and a cell in bold has just changed.
Words used on the pattern pages
- Pointer
- The cell the program is working on. A pattern says where it must start and where it ends.
- Read, changed, put back
- In the small drawings below: “read” means the program looks at the cell, “changed” means it holds a new value at the end, and “put back” means it was used on the way and holds its old value again.
- Scratch cell
- A helper cell. It must be zero before the pattern runs, and the pattern leaves it zero. The tests check both.
- Cost
- How many steps the pattern takes. A plain step is one command. An optimised step is counted by an interpreter that folds simple loops. The two can differ a thousand times; What costs what shows why.
- Cell size
- Most patterns work on any cell size. Where a pattern needs cells that wrap round, or a fixed width of 8, 16 or 32 bits, its page says so under “What it needs”.
Find a pattern
38 patterns, 111 forms. Every one is tested.
Under each pattern is the tape before and after its first form, for the values in its figure.
- the pointer is on this cell
- the program reads this cell
- the cell holds a new value
I want to put a value somewhere First moves
- Set a cell to zeroEmpties the cell under the pointer, whatever it held.
[-]before5after0 - Set a constantPuts a fixed number in a cell, in as few characters as possible.
>++++++++[-<++++++++>]<+before00after650 - Move a valueAdds a cell to another cell and leaves the first one empty.
[->+<]before40after04 - Copy a valueAdds a cell to another cell and keeps the first one.
[->+>+<<]>>[-<<+>>]<<before300after330 - Add and subtract two cellsAdds one cell to another, or takes it off.
>[-<+>]<before34after70 - Swap two cellsExchanges the values of two cells.
[->>+<<]>[-<+>]>[-<+>]<<before270after720
I want to decide or repeat Control
- If non-zeroRuns a piece of program once when a cell is not zero.
[>+<[-]]before50after01 - If zeroRuns a piece of program once when a cell is zero.
>+<[>-]>[>>+<<->]<<before0000after0001 - If and elseRuns one piece of program when a cell is not zero and another piece when it is zero.
>+<[>>>+<<-]>[>>>+<<<->]<<before50000after50010 - WhileRepeats a piece of program as long as a condition holds.
,----------[>+<,----------]before00after05reads hello↵ - A counted loopRuns a piece of program a given number of times.
>++++++[->+++++++<]<[->>.<<]before500after0042prints ***** - Not, and, or on flagsCombines yes-or-no values, where 0 means no and 1 means yes.
>+<[->-<]>[-<+>]<before10after00 - EqualSets a flag to 1 when two cells hold the same number.
[->-<]+>[<->[-]]<before77after10 - Less thanSets a flag to 1 when the first number is smaller than the second.
[->>+<[->-]>[<<[-]>>->]<<<]>[[-]>+<]<before3500after0010 - Find a zero cell or a markerMoves the pointer to the nearest cell with a given value, however far away it is.
[>]before314150after314150
I want to calculate Arithmetic
- Multiply by a constantAdds a fixed multiple of a cell to another cell.
[->+++<]before50after015 - Multiply two cellsMultiplies two numbers that are both on the tape.
[->>>+<<<]>>>[<<[-<+>>+<]>[-<+>]>-]<<<before6700after42700 - Divide, with remainderDivides one cell by another, or by a constant, and gives the quotient and the remainder.
>>+<<[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>[-]>-<<before1750000after002300 - SquareReplaces a number by its square.
[->+<]>[-[->+<<++>]<+>>[-<+>]<]<before500after2500 - Powers of two, and powersWorks out 2 to the power n, or x to the power y.
>+<[->[->++<]>[-<+>]<<]before500after0320 - Saturating subtractSubtracts, but stops at zero instead of wrapping round.
[->>+<[->-]>[<<[-]>>->]<<<]before5300after0000
I want to keep many values Data
- Walk an array of recordsDoes the same job on every record of a row, with one copy of the program.
[>[->++<]>>]<<<[<<<]>>>before00013011014…after00010610210… - Store and fetch by indexReads or writes item number i of an array, when i is a value on the tape.
>[->>+<<]>>[-[->>>+<<<]+>>>]>[->+<<+>]<[->+<]<<<[->>>>>[-<<<… 94 charactersbefore02007003009…after00907003009… - A stackKeeps values in a pile: you add to the top and take from the top.
>+++before580…after583… - Read a line into memoryReads letters and stores them in cells next to each other, up to a newline or the end of the input.
>,----------[++++++++++>,----------]before0000000after01041011081081110reads hello↵ - Reverse a stringPrints a text back to front.
>,[>,]<[.<]before000000…after0115116114101115…reads stressed, prints desserts - Compare two stringsReads two lines and prints Y if they are the same and N if not.
>>>,----------[++++++++++>>>,----------]<<<[<<<]>>>>>,------… 230 charactersbefore0…000000000…after0…000000000…reads tea↵ten↵, prints N
I want to read or print Input and output
- Print a constant stringPrints a fixed text with as short a program as possible.
+++++++++++[>+++>++++>+++++++>++++++++>+++++++++>++++++++++<… 126 charactersbefore0000000after033447287100108prints Hello, World! - Print a number in decimalPrints the value of a cell as digits, for example 173.
>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>+++++… 204 charactersbefore17300000000after17300000000prints 173 - Read a decimal numberReads digits such as 173 and puts the number they spell in a cell.
>,----------[>++++++[-<------>]<--<[->>++++++++++<<]>>[-<<+>… 82 charactersbefore000after17300reads 173↵ - Echo until the end of the inputPrints everything it reads and stops when the input ends, on any interpreter.
>,[+[-.[-],>]<]before000after000reads abc, prints abc
I want it to be fast on large numbers Advanced: wrapping cells of a known width
- The bit peelReads a number bit by bit, at a fixed cost per bit, instead of counting it down unit by unit.
>>>>>+<<<<<[->+>++++++++++++++++++++++++++++++++++++++++++++… 828 charactersbefore10000…00…0000…after000…00…0010… - Shift, round and clampDivides a signed number by a power of two, rounds it, and keeps the result between −128 and 127.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++… 11,212 charactersbefore11,86800000000…after0930000000… - Signed multiplication by shift-and-addMultiplies two numbers from the tape, negative ones too, at a cost that does not depend on the numbers.
[->>>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++… 3,093 charactersbefore−25−60000000…after−25−601,50000000… - Table lookup as a decision treeGives entry number x of a fixed table, in a number of steps that depends on the size of the table and not on x.
[->>>>>+++++++++++++++++++++++++++++++++++++++++++++++++++++… 4,035 charactersbefore50000000000after525000000000 - One copy of code over many framesRuns one piece of program at many places on the tape, so the program text holds it once instead of many times.
+[->>+>>>>+>>>>+>>>>+<<<<<<<<<<<<<<]>>[->[->+++>+<<]>>[-<<+>… 70 charactersbefore0…020003000…after0…026003900… - Pick the largest by a raceFinds which of many numbers is the largest, without comparing any two of them.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+[-<<<<<<<<<<<<<<<<<<<<<<<<<+>… 663 charactersbefore0…0…05130…02…after0…0…0000…00… - Write many numbers compactly as digitsPuts a long row of fixed numbers on the tape with a few characters per number, instead of one plus sign per unit.
>>>>+>+>+>+>+>++++++++++<++++<<+++++++<++<+++[[-<+++++>]>]<<… 101 charactersbefore0000000…after01006420031127250…
Where to go next
- Start here teaches the language from the eight commands; the Brainfuck basics are ten short lessons, and Build toward a chatbot builds the advanced patterns on this page.
- The playground is a blank page to run your own program, and the challenges check your answers.
- The reference defines the words, and How it works follows one question through a chat model written with the advanced patterns.