Müller designed Brainfuck with the goal of implementing the smallest possible compiler,6 inspired by the 1024-byte compiler for the FALSE programming language.78 Müller's original compiler was implemented in Motorola 68000 assembly on the Amiga and compiled to a binary with a size of 296 bytes. He uploaded the first Brainfuck compiler to Aminet in 1993. The program came with a "Readme" file, which briefly described the language, and challenged the reader "Who can program anything useful with it? :)". Müller also included an interpreter and some examples. A second version of the compiler used only 240 bytes.9
The language consists of eight commands. A brainfuck program is a sequence of these commands, possibly interspersed with other characters (which are ignored). The commands are executed sequentially, with some exceptions: an instruction pointer begins at the first command, and each command it points to is executed, after which it normally moves forward to the next command. The program terminates when the instruction pointer moves past the last command.
The brainfuck language uses a simple machine model consisting of the program and instruction pointer, as well as a one-dimensional array of at least 30,000 byte cells initialized to zero; a movable data pointer (initialized to point to the leftmost byte of the array); and two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).
The eight language commands each consist of a single character:
[ and ] match as parentheses usually do: each [ matches exactly one ] and vice versa, the [ comes first, and there can be no unmatched [ or ] between the two.
Brainfuck programs are usually difficult to comprehend. This is partly because any mildly complex task requires a long sequence of commands and partly because the program's text gives no direct indications of the program's state. These, as well as Brainfuck's inefficiency and its limited input/output capabilities, are some of the reasons it is not used for serious programming. Nonetheless, like any Turing-complete language, Brainfuck is theoretically capable of computing any computable function or simulating any other computational model if given access to an unlimited amount of memory and time.11 A variety of Brainfuck programs have been written.12 Although Brainfuck programs, especially complicated ones, are difficult to write, it is quite trivial to write an interpreter for Brainfuck in a more typical language such as C due to its simplicity. Brainfuck interpreters written in the Brainfuck language itself also exist.1314
As a first, simple example, the following code snippet will add the current cell's value to the next cell: Each time the loop is executed, the current cell is decremented, the data pointer moves to the right, that next cell is incremented, and the data pointer moves left again. This sequence is repeated until the starting cell is 0.
This can be incorporated into a simple addition program as follows:
The following program prints "Hello World!" and a newline to the screen:
For readability, this code has been spread across many lines, and blanks and comments have been added. Brainfuck ignores all characters except the eight commands +-<>[],. so no special syntax for comments is needed (as long as the comments do not contain the command characters). The code could just as well have been written as:
This program enciphers its input with the ROT13 cipher. To do this, it must map characters A-M (ASCII 65–77) to N-Z (78–90), and vice versa. Also it must map a-m (97–109) to n-z (110–122) and vice versa. It must map all other characters to themselves; it reads characters one at a time and outputs their enciphered equivalents until it reads an EOF (here assumed to be represented as either -1 or "no change"), at which point the program terminates.
In 2024, a Google research project used a slightly modified 10-command version of Brainfuck as the basis of an artificial digital environment. In this environment, they found that replicators arose naturally and competed with each other for domination of the environment.15
Easter, Brandee (2020-04-02). "Fully Human, Fully Machine: Rhetorics of Digital Disembodiment in Programming". Rhetoric Review. 39 (2): 202–215. doi:10.1080/07350198.2020.1727096. ISSN 0735-0198. S2CID 219665562. /wiki/Doi_(identifier) ↩
Temkin, Daniel (2017-09-01). "Language without code: intentionally unusable, uncomputable, or conceptual programming languages". Journal of Science and Technology of the Arts. 9 (3): 83–91. doi:10.7559/citarj.v9i3.432. ISSN 2183-0088. Archived from the original on 2024-07-09. Retrieved 2024-02-11. https://revistas.ucp.pt/index.php/jsta/article/view/7297 ↩
Haupt, Michael. "Implementing Brainfuck in COLA." ↩
Cox, Geoff, and Alex McLean. "Not Just for Fun." Bloomsbury Academic, 2014. 157-173. ↩
"brainfuck - Esolang". esolangs.org. Archived from the original on 2018-10-23. Retrieved 2024-02-07. https://esolangs.org/wiki/Brainfuck ↩
"The Brainfuck Programming Language". Muppetlabs.com. Archived from the original on 2023-05-03. Retrieved 2023-04-30. https://muppetlabs.com/~breadbox/bf/ ↩
"The FALSE Programming Language — Wouter van Oortmerssen". Strlen.com. 2013-08-03. Archived from the original on 2023-04-30. Retrieved 2023-04-30. https://strlen.com/false-language/ ↩
Tamedia TX 2017 - Livestream Werdino. 13 June 2017. Archived from the original on 11 August 2023. Retrieved 30 August 2024. https://www.youtube.com/watch?v=gjm9irBs96U&t=8722s ↩
"Aminet - dev/lang/brainfuck-2.lha". Aminet. Archived from the original on 2023-04-30. Retrieved 2023-04-30. https://aminet.net/package.php?package=dev/lang/brainfuck-2.lha ↩
Alternatively, the ] command may instead be translated as an unconditional jump to the corresponding [ command, or vice versa; programs will behave the same but will run more slowly, due to unnecessary double searching. ↩
"BF is Turing-complete". Iwriteiam.nl. Archived from the original on 2023-05-23. Retrieved 2023-04-30. https://iwriteiam.nl/Ha_bf_Turing.html ↩
"Index of /esoteric/brainfuck/bf-source/prog". sange.fi. 2002-01-22. Archived from the original on 2023-04-30. Retrieved 2023-04-30. https://sange.fi/esoteric/brainfuck/bf-source/prog/ ↩
"BF interpreter written in BF". Iwriteiam.nl. Archived from the original on 2024-09-22. Retrieved 2023-04-30. https://iwriteiam.nl/Ha_bf_inter.html ↩
"brainfuck interpreter". Daniel B. Cristofani. Archived from the original on 2024-09-22. Retrieved 2024-06-19. https://www.brainfuck.org/dbfi.b ↩
Blaise Agüera y Arcas; Alakuijala, Jyrki; Evans, James; Laurie, Ben; Mordvintsev, Alexander; Niklasson, Eyvind; Randazzo, Ettore; Versari, Luca (2024). "Computational Life: How Well-formed, Self-replicating Programs Emerge from Simple Interaction". arXiv:2406.19108 [cs.NE]. /wiki/ArXiv_(identifier) ↩