Brainfuck
Eight characters, a tape and a pointer. Everything else is a comment. The most copied esolang ever made.
Urban Müller, 1993 · Turing complete: with unbounded memory · ●○○ easy · esolangs.org
Tested with 6 examples · Hello World on Try It Online: identical · Test method
Run it
Hello World
The classic program. It builds the letters in a few cells with one nested loop, then prints them.
Expected output
Hello World!
Output
The idea
Urban Müller wanted a language with the smallest possible compiler for AmigaOS 2.0. The result had eight one-character instructions, and his compiler was 240 bytes. FALSE, the language that inspired it, needed 1024 (esolangs.org).
Read the story
It closely resembles Corrado Böhm's P′′ (1964), the first language proven to be universal with only a loop and a few moves; how much Müller knew of it is not known. Brainfuck added input and output, and a mathematical curiosity became something people play with.
Its secret is that it is just barely usable. Hard enough to be a puzzle, easy enough that anyone can write an interpreter in an evening. That is why there are hundreds of derivatives.
The machine
A tape of byte cells (classically 30,000), all zero, and a pointer at cell 0.
| Char | Does |
|---|---|
+ - | Add or subtract 1 from the current cell (wraps at 256 in most implementations) |
> < | Move the pointer right or left |
. | Print the current cell as a character |
, | Read one character into the current cell |
[ | If the cell is 0, jump past the matching ] |
] | If the cell is not 0, jump back to the matching [ |
Every other character is ignored. So comments are free, as long as they avoid the eight symbols.
Example
Multiply two numbers.
Multiply two numbers. The first cell holds 21; [>+<-] moves it into the next cell; [<++>-] moves it back, doubled.
, read a byte (we feed it 21)
[>+<-] move cell 0 into cell 1
>[<++>-] move cell 1 back into cell 0, two at a time
<. print cell 0: byte 42
The idiom [->+<] ("drain this cell into the next") is the for loop of Brainfuck. Almost every program is built from a handful of these moves.
The Hello World from the esolangs.org article (CC0) uses nested loops to build several letters at once:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
Commands
| Command | What it does | Kernel tokens |
|---|---|---|
> | move the pointer one cell right | 1 MOVE |
< | move the pointer one cell left | -1 MOVE |
+ | add 1 to the cell (255 + 1 wraps to 0) | PTR LOAD 1 ADD 256 MOD PTR STORE |
- | take 1 from the cell (0 - 1 wraps to 255) | PTR LOAD 255 ADD 256 MOD PTR STORE |
. | print the cell as a byte | PTR LOAD EMIT |
, | read one byte into the cell (0 at the end of input) | READ 1 ADD DUP IF 1 SUB THEN PTR STORE |
[ | if the cell is 0, jump past the matching ] | PTR LOAD IF BEGIN |
] | if the cell is not 0, jump back to the matching [ | PTR LOAD NOT UNTIL THEN |
Read the kernel column from left to right as a stack machine. PTR LOAD pushes the value of the current cell, 1 ADD 256 MOD adds one and wraps, and PTR STORE writes it back. [ and ] are halves of two pairs; [ is IF BEGIN and ] is UNTIL THEN. The kernel matches the halves after reading, so one bracket can open two constructs. See code as data for how a dictionary like this defines a whole language.
A first program, step by step
The letter A is byte 65.
Print the letter A
The letter A is byte 65. Writing 65 plus signs works, but a loop is shorter. 8 times 8 is 64, and one more is 65.
Expected output
A
Output
++++++++sets cell 0 to 8.[>++++++++<-]adds 8 to cell 1 and takes 1 from cell 0. It runs 8 times, so cell 1 ends at 64.>+.moves to cell 1, makes it 65 and prints it, an A.
Classic programs
Read a byte; while it is not 0, print it and read the next.
Cat: copy input to output
Read a byte; while it is not 0, print it and read the next. At the end of input , stores 0 and the loop ends.
Expected output
echo
Output
Reverse the input
Read every byte into its own cell, moving right. Then walk back left and print each one.
Expected output
cba
Output
Add two digits
Read two digits into two cells. The loop moves the first value onto the second, so the cell holds the codes 51 + 52 = 103. Taking 48 (the code of 0) once gives 55, the character 7.
Expected output
7
Output
Quirks
brainfuck leaves some things to the implementation.
brainfuck leaves some things to the implementation. Each item gives the choice of this implementation first.
- Cell size. Cells are 8 bits and wrap. Some interpreters use 16 or 32 bits, or numbers without a limit.
- End of input.
,stores 0 at the end of input. Others store −1 (255) or leave the cell unchanged. A cat program must be written for one rule. - The left edge. The tape wraps. Some interpreters stop with an error when the pointer goes left of the first cell.
- Comments. Every character that is not a command is ignored. That is why the kernel reads brainfuck with
plain=off, so plain words in a comment do not run.
6 dialects of brainfuck make one choice each, following the implementation issues listed on esolangs.org. The same program can print different things in each.
How big is a cell?
A cell-size test from esolangs.org. It reads no input, so the end-of-input dialects print what 8-bit brainfuck prints; only the cell dialects change it.
Expected output
8 bit cells
| Dialect | Output |
|---|---|
| brainfuck (8-bit cells) | 8 bit cells
|
| brainfuck (end of input leaves the cell) | 8 bit cells
|
| brainfuck (end of input stores -1) | 8 bit cells
|
| brainfuck (16-bit cells) | 16 bit cells
|
| brainfuck (32-bit cells) | 32 bit cells
|
| brainfuck (unbounded cells) | 32 bit cells
|
Output
Try this
- Write a program that prints
A(65) in fewer than 20 instructions. Hint: a loop of 8 × 8 + 1. - Write
catthat stops at a newline instead of end of input. - Hard: write a Brainfuck program that prints its own source.
Patterns it teaches
- Turing tarpit: everything is possible, nothing is easy.
- Turing completeness: with an unbounded tape it is universal; with 30,000 cells it is a (huge) finite machine.
- Quine: Brainfuck quines exist, but they have to encode their own text as cell values first. A good first look at why quines need a "data half" and a "code half".
- Encoding tricks: Ook!, Unary and Lenguage are all Brainfuck in disguise.
- Metacircular interpreter: Brainfuck interpreters written in Brainfuck exist, a few hundred characters long.
- Artificial life: bff, the "Computational Life" experiment, threw random Brainfuck-like tapes in a soup and watched self-replicators appear.
Family and nearest relatives
89 languages are substitutions of brainfuck, which only rename the eight commands.
89 languages are substitutions of brainfuck, which only rename the eight commands. See why so many esolangs are brainfuck in other clothes.
Run on the same 21 brainfuck programs in four spellings (as written, glued together, with English prose between the tokens, and in capitals), 42 substitutions behave as brainfuck in every spelling. 34 behave as brainfuck except in capitals, because their tokens are case-sensitive.
Some relatives change more than the spelling.
- Ook! spells each command as a pair of words. Blub does the same.
- Self-modifying Brainfuck keeps the program on the tape, so the program can change itself.
- Boolfuck, Smallfuck and Brainbool work on bits instead of bytes.
- PATH, Minimal-2D, Brainloller and SNUSP draw brainfuck on a grid.
- Wordfuck reads a command from the length of each word.
Further
- esolangs.org/wiki/Brainfuck
- Corrado Böhm, "On a family of Turing machines and the related programming language" (1964), the P′′ paper.
- Algorithms and idioms: esolangs.org/wiki/Brainfuck_algorithms
Substitutions
89 languages rename Brainfuck’s commands and change nothing else.
89 languages rename Brainfuck’s commands and change nothing else.
See also
Brainfuck
Cell-based
Write your own in Run Convert a program from Brainfuck
Implementation
Kernel translation, .shib file and command line
Runs on the brainfuck interpreter. Id: brainfuck.
Command line
shib run brainfuck -e '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.'shib run shib:brainfuck -e '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.'shib manual brainfuckKernel translation
| Command | Kernel instructions |
|---|---|
+ | PTR LOAD 1 ADD 256 MOD PTR STORE |
- | PTR LOAD 255 ADD 256 MOD PTR STORE |
> | 1 MOVE |
< | -1 MOVE |
. | PTR LOAD EMIT |
, | READ 1 ADD DUP IF 1 SUB THEN PTR STORE |
[ | PTR LOAD IF BEGIN |
] | PTR LOAD NOT UNTIL THEN |
Kernel definition
#! shibboleth 3 seed=brainfuck syllables= plain=off
brainfuck as a front end: eight reading rules over the pointer and memory; [ and ] are halves of IF/BEGIN pairs
"+" => PTR LOAD 1 ADD 256 MOD PTR STORE
"-" => PTR LOAD 255 ADD 256 MOD PTR STORE
">" => 1 MOVE
"<" => -1 MOVE
"." => PTR LOAD EMIT
"," => READ 1 ADD DUP IF 1 SUB THEN PTR STORE
"[" => PTR LOAD IF BEGIN
"]" => PTR LOAD NOT UNTIL THENSource
- Interpreter
tools/convert/languages.js- Kernel definition
shibboleth/witness/brainfuck.shibonshibboleth/kernel.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org