ArticleslgStudy

science

Brainfuck

Brainfuck is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Brainfuck rather than just read about it. In short: Brainfuck is an esoteric programming language created in 1993 by Swiss student Urban Müller. Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer.

Brainfuck — main illustration
Brainfuck — illustration

Key takeaways

  • Brainfuck belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Brainfuck to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Brainfuck from memory before moving on to harder problems.

Reference excerpt

Brainfuck is an esoteric programming language created in 1993 by Swiss student Urban Müller. Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer. Brainfuck is an example of a so-called Turing tarpit: it can be used to write any program, but it is not practical to do so because it provides so little abstraction that the programs get very long or complicated. While Brainfuck is fully Turing-complete, it is not intended for practical use but to challenge and amuse programmers. Brainfuck requires one to break down programs into small and simple instructions. The language takes its name from the slang term brainfuck, which refers to things so complicated or unusual that they exceed the limits of one's understanding, as it was not meant or made for designing actual software but to challenge the boundaries of computer programming. Because the language's name contains profanity, many substitute names are used, such as brainf*ck, brainfsck, branflakes, brainoof, brainfrick, BrainF, brainfreeze, and BF. Brainfuck also has many derivatives and equivalents, including !!Fuck, Binary Brainfuck, 100BF, BaguaFuck, and several others

History Müller designed Brainfuck with the goal of implementing the smallest possible compiler, inspired by the 1024-byte compiler for the FALSE programming language. 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.

Language design The language consists of eight commands. A Brainfuck program is a sequence of these commands. 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. In the canonical abstract machine model for Brainfuck, a program operates on a one-dimensional tape of memory cells indexed from zero and unbounded to the right. Each cell contains an 8-bit unsigned integer (0–255) and is initialized to zero. Arithmetic on cells wraps modulo 256, so incrementing 255 produces 0 and decrementing 0 produces 255. A data pointer (initialized to point to the leftmost byte of the array) indicates the current cell on the tape and can move left or right. Moving left from the first cell (index 0) is undefined. The language provides eight commands that modify the tape, move the pointer, perform input/output (using the ASCII character encoding) or control loops. The eight language commands each consist of a single character:

Notes The much older language P′′ already featured three of these commands, >, [, and ], denoting them R, (, and ) instead, while the fourth P′′ command, denoted λ, combined + and < into one. As the Turing Machine that it modeled, P′′ lacked I/O commands. Loops [ … ] can be nested arbitrarily and all brackets must be properly matched, making the set of valid programs a context-free language. Many practical implementations allocate a finite array of cells (commonly at least 30 000 bytes) and use 8‑bit cells with wraparound, although neither the exact cell count nor wraparound behavior is required by all interpreters. In implementations with 8-bit unsigned cells and arithmetic wrapping at 256, one of the + or - instructions is redundant. Incrementing a cell by one is equivalent to decrementing it 255 times in succession, and decrementing by one is equivalent to incrementing it 255 times in succession. Accordingly, either the + instruction may be expressed as −255, or the - instruction as +255. 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. A variety of Brainfuck programs have been written. 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.

Examples

Hello World! The following program prints "Hello World!" and a newline to the screen. Brainfuck uses ASCII for letters.

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:

Another example of 'Hello World' code is presented here. This version highlights the complexity of the language. For example, after printing 'e', it is necessary to increment cell #3 to switch to the letter 'l'. Displaying the second 'l' requires no further modifications, as cell 3 already contains 'l'.

Adding two values 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: c0final=((2mod256)+(5mod256))mod256=7mod256=7

… excerpt ends here. Continue reading the full article.

Illustrations

Brainfuck illustration

Worked examples

Example 1 — a first encounter with Brainfuck

Start with the simplest possible case. Write down what Brainfuck claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Brainfuck before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Brainfuck ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Brainfuck

In research
Brainfuck appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Brainfuck in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Brainfuck is common in secondary-school and first-year university syllabi. It links to neighbouring topics English profanity, Esoteric programming languages, Non-English-based programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Brainfuck outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Brainfuck” →

How to study Brainfuck in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Brainfuck means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Brainfuck out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Brainfuck in simple terms?

Brainfuck is an esoteric programming language created in 1993 by Swiss student Urban Müller. Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer.

Why does Brainfuck matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Brainfuck?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Brainfuck.

Tags

  • English profanity
  • Esoteric programming languages
  • Non-English-based programming languages
  • Programming languages
  • Programming languages created in 1993

Keep exploring