Brainfuck

Appeared in:
1993
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.bf
Dialects:
Versions and implementations (Collapse all | Expand all):
Programming language

Brainfuck is a minimalistic esoteric programming language.

Brainfuck was created in 1993 by Urban Müller, and his implementation remains a de facto standard. Its specification is as simple as 8 commands, so it hasn’t passed standardization, and it has lots of amateur implementations, none of which are of commercial value.

However, Brainfuck has spawned lots of dialects, varying in the set of commands, in the way they are written or in minor implementation details like cell size or erroneous behavior.

Brainfuck was invented in an attempt to create a Turing-complete programming language with the smallest possible compiler. The original compiler was as small as 240 bytes, and some later implementations are smaller than 200 bytes.

Brainfuck is probably the most famous of esoteric programming languages. It is indeed Turing-complete and thus theoretically capable of executing any real-life task. However, this language is absolutely unfit for any real-life development: doing even the simplest things turns out to be a challenge for the programmer, so it is used only as math model or a kind of puzzle.

Brainfuck creation was inspired by FALSE. Its six commands (except for I/O related ones) are exactly the same as the ones used in P”, but it’s unknown whether this language influenced Brainfuck development.

Brainfuck uses a machine model similar to Turing machine, consisting of the following elements:

  • program is a sequence of one-character language commands and (optionally) other characters which are ignored;
  • instruction pointer points at the command to be executed on the next step, and moves to another (typically next) command afterwards;
  • memory is modeled with a one-dimensional array of cells, each cell stores one byte and is initialized with 0;
  • data pointer points at the current memory cell; is initialized to point to the leftmost cell of the array, and can move or modify the value stored at the cell it points to (in accordance to the commands);
  • input and output streams are sequences of bytes in ASCII encoding.

The commands of the language are the following:

  • + : increment the value stored in the current cell;
  • - : decrement the value stored in the current cell;
  • > : increment the data pointer (move it to the next cell to the right);
  • < : decrement the data pointer (move it to the next cell to the left);
  • [ : “begin loop”: if the value in the current cell is positive, increment the instruction pointer (move it to the next command to the right), otherwise move it to the next command to the right of the matching ] command;
  • ] : “end loop”: if the value in the current cell is zero, increment the instruction pointer, otherwise move it to the next command to the right of the matching [ command. Can also be interpreted as unconditional jump to the matching [ command, since [ performs an extra check itself;
  • . : print the value stored at the current cell to the output stream as a character with corresponding ASCII-code;
  • , : read the character from the input stream and store its ASCII-code to the current cell.

Note that due to language specifics [ and ] commands are used not only for implementing loops but in most actions which are elementary in other languages (like value assignment, mathematical and logical operations, if-then-else constructs etc.).

All characters other than these eight are ignored, so the comments can be added freely as long as they don’t contain command characters.

Examples:

Hello, World!:

Example for versions EsCo 0.511 (Brainfuck), Müller's Brainfuck 2.0

There are lots of ways to say “Hello, World!” in Brainfuck. Here is the simplest one: use only one memory cell, and change its value to ASCII-code of each letter in row. Each line of the example prints one letter.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
+++++++++++++++++++++++++++++.
+++++++.
.
+++.
-------------------------------------------------------------------.
------------.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++++++++.
+++.
------.
--------.
-------------------------------------------------------------------.

Hello, World!:

Example for versions EsCo 0.511 (Brainfuck), Müller's Brainfuck 2.0

In this example we use three memory cells — first for uppercase letters ‘H’ and ‘W’, second for lowercase letters and third for special characters ‘,’, ‘ ‘ and ‘!’ — and three index cells to shorten the notation of ASCII-codes changes. The memory used looks like this:

(index cell 1) (uppercase letters cell) (index cell 2) (lowercase letters cell) (index cell 3) (special characters cell)

++++++[>++++++++++++<-]>.
>++++++++++[>++++++++++<-]>+.
+++++++.
.
+++.
>++++[>+++++++++++<-]>.
<+++[>----<-]>.
<<<<<+++[>+++++<-]>.
>>.
+++.
------.
--------.
>>+.

Fibonacci numbers:

Example for versions EsCo 0.511 (Brainfuck), Müller's Brainfuck 2.0

This example uses iterative definition of Fibonacci numbers. A high-level description of what it does is: store two last numbers in variables c4 and c5 (initially c4=0, c5=1), print the number stored in c5 (this operation takes the major part of the code), calculate next number (c6 = c5+c4), and move the numbers sequence one number back (c4 = c5, c5 = c6). A low-level description is given in the comments, notation “cXvY” meaning that after execution of the commands in the line the data pointer is at cell X, and the value at this cell is Y. A total of 12 memory cells is used.

This example uses one minor cheat: classic Brainfuck interpreter uses byte variables to store values of memory cells, so Fibonacci numbers 14 through 16 will cause overflow. Writing long arithmetics in Brainfuck is a bit of overkill, so in this example we assume that memory cells can store integer values.

++++++++++++++++++++++++++++++++++++++++++++		c1v44 : ASCII code of comma
>++++++++++++++++++++++++++++++++			c2v32 : ASCII code of space
>++++++++++++++++					c3v11 : quantity of numbers to be calculated
>							c4v0  : zeroth Fibonacci number (will not be printed)
>+							c5v1  : first Fibonacci number
<<							c3    : loop counter
[							block : loop to print (i)th number and calculate next one
>>							c5    : the number to be printed

							block : divide c5 by 10 (preserve c5)
>							c6v0  : service zero
>++++++++++						c7v10 : divisor
<<							c5    : back to dividend
[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]			c5v0  : divmod algo; results in 0 n d_n%d n%d n/d
>[<+>-]							c5    : move dividend back to c5 and clear c6
>[-]							c7v0  : clear c7

>>							block : c9 can have two digits; divide it by ten again
>++++++++++						c10v10: divisor
<							c9    : back to dividend
[->-[>+>>]>[+[-<+>]>+>>]<<<<<]				c9v0  : another divmod algo; results in 0 d_n%d n%d n/d
>[-]							c10v0 : clear c10
>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]c12v0 : print nonzero n/d (first digit) and clear c12
<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]] c11v0 : print nonzero n%d (second digit) and clear c11

<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-]	c8v0  : print any n%d (last digit) and clear c8
<<<<<<<.>.                                              c1c2  : print comma and space
							block : actually calculate next Fibonacci in c6
>>[>>+<<-]						c4v0  : move c4 to c6 (don't need to preserve it)
>[>+<<+>-]						c5v0  : move c5 to c6 and c4 (need to preserve it)
>[<+>-]							c6v0  : move c6 with sum to c5
<<<-							c3    : decrement loop counter
]
<<++...							c1    : output three dots

Factorial:

Example for versions EsCo 0.511 (Brainfuck), Müller's Brainfuck 2.0

This example uses iterative definition of factorial. Last calculated factorial is stored in variable c6 and on each step it is multiplied by next number (stored in c5). A low-level description is given in the comments, notation “cXvY” meaning that after execution of the commands in the line the data pointer is at cell X, and the value at this cell is Y. A total of 13 memory cells is used.

This example uses one minor cheat: classic Brainfuck interpreter uses byte variables to store values of memory cells, so 6! and further will cause overflow. Writing long arithmetics in Brainfuck is a bit of overkill, so in this example we assume that memory cells can store integer values. Besides, factorial length grows fast along with execution time of Brainfuck program, so the above code is limited to calculating and printing first 7 factorials.

+++++++++++++++++++++++++++++++++			c1v33 : ASCII code of !
>++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++				c2v61 : ASCII code of =
>++++++++++						c3v10 : ASCII code of EOL
>+++++++						c4v7  : quantity of numbers to be calculated
>							c5v0  : current number (one digit)
>+							c6v1  : current value of factorial (up to three digits)
<<							c4    : loop counter
[							block : loop to print one line and calculate next
>++++++++++++++++++++++++++++++++++++++++++++++++.	c5    : print current number
------------------------------------------------	c5    : back from ASCII to number
<<<<.-.>.<.+						c1    : print !_=_

>>>>>							block : print c6 (preserve it)
>							c7v0  : service zero
>++++++++++						c8v10 : divizor
<<							c6    : back to dividend
[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]			c6v0  : divmod algo borrowed from esolangs; results in 0 n d_n%d n%d n/d
>[<+>-]							c6    : move dividend back to c6 and clear c7
>[-]							c8v0  : clear c8

>>							block : c10 can have two digits; divide it by ten again
>++++++++++						c11v10: divizor
<							c10   : back to dividend
[->-[>+>>]>[+[-<+>]>+>>]<<<<<]				c10v0 : another divmod algo borrowed from esolangs; results in 0 d_n%d n%d n/d
>[-]							c11v0 : clear c11
>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]c13v0 : print nonzero n/d (first digit) and clear c13
<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]] c12v0 : print nonzero n%d (second digit) and clear c12
<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-]	c9v0  : print any n%d (last digit) and clear c9

<<<<<<.							c3    : EOL
>>+							c5    : increment current number
							block : multiply c6 by c5 (don't preserve c6)
>[>>+<<-]						c6v0  : move c6 to c8
>>							c8v0  : repeat c8 times
[
<<<[>+>+<<-]						c5v0  : move c5 to c6 and c7
>>[<<+>>-]						c7v0  : move c7 back to c5
>-
]
<<<<-							c4    : decrement loop counter
]

Hello, World!:

Example for versions Müller's Brainfuck 2.0

This example is a Brainloller translation of this example. Since Brainloller is a fully graphical language, no source code is available, see screenshots instead.

"Hello, World!" example in Brainloller
"Hello, World!" example in Brainloller

"Hello, World!" example in Brainloller (10x scale)
"Hello, World!" example in Brainloller (10x scale)

Fibonacci numbers:

Example for versions Müller's Brainfuck 2.0

This example is a Brainloller translation of this example. Since Brainloller is a fully graphical language, no source code is available, see screenshots instead.

Fibonacci numbers example in Brainloller
Fibonacci numbers example in Brainloller

Fibonacci numbers example in Brainloller (10x scale)
Fibonacci numbers example in Brainloller (10x scale)

Hello, World!:

Example for versions Müller's Brainfuck 2.0

This example is a translation of this example into Unary. The program itself is too large, so it is given in shortened way.

A string of 
708184005756841022918598670049178934705323143517361395031673227349803938380
119378597780037353721967636097362645175347036417214959141923667629285233360
306016978751166690464736541968556 zeroes (approximately 7*10^182).

Fibonacci numbers:

Example for versions Müller's Brainfuck 2.0

The program itself is too large, so it is given in shortened way. This example is a translation of Brainfuck example.

A string of 
146778148267671308907956810331954494567788969820594569869966345643952713144
716974835554679004232198811425384864927587749892052914319949694507679080918
662111668706252645905597146857061868763596677983948203224834326028677131466
814323099384842068831692029352209655371798175735992788874417787727414767365
600708388513171998134124513036377960362194431944262896105838957344640161915
106378867996851411865254464299481964724009334722033995112813417289458551426
925973669722270280516592327343992579166227546099835941334220 zeros (approximately 1.5*10^509)

Hello, World!:

Example for versions Müller's Brainfuck 2.0

This example is Pi translation of this one.

3.141592653589793238462623382272502824197169299275107820904924592337816406386238
99262833482534311206728234808621328230264709314460935058223872535941812844111745
00841022019385311055296426229289549302819244388109726652334471204756422337867231
65221231909345628566933460342610454226248213391607264249148273720587036656315582
17288153092396282225439171532436789559536003133023024882044652108412695192151163
94330573703656595909530921261173839326137921051125420742623799227495273538857227
24892227938133011749109833675362442656243086321294946795024737130702479860343702
77453921711629317375838467480846766440513202056822724526351082178577132275778260
91736271767204684409312229532301462492853110307922896892089235450199501120290219
65862034218129813624774731309964518707241349993993372978039951049734732816036348
59504445345544690330263252250825304468003522193158817101

Hello, World!:

Example for versions EsCo 0.511 (Brainfuck)

This example is Ook! translation of second Brainfuck example.

Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook. Ook? 
Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook. Ook! Ook! Ook? Ook! Ook. Ook? 
Ook! Ook. Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook. Ook? Ook. Ook. Ook. Ook. 
Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
Ook? Ook. Ook! Ook! Ook? Ook! Ook. Ook? Ook. Ook. Ook! Ook. Ook. Ook. Ook. Ook. 
Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook! Ook. Ook. Ook. 
Ook. Ook. Ook. Ook. Ook! Ook. Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
Ook! Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook. Ook! Ook! Ook? Ook! 
Ook. Ook? Ook! Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook. Ook? 
Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook? Ook. Ook! Ook! Ook? Ook! Ook. Ook? 
Ook! Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. 
Ook. Ook. Ook! Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
Ook? Ook. Ook! Ook! Ook? Ook! Ook. Ook? Ook! Ook. Ook. Ook? Ook. Ook? Ook! Ook. 
Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! 
Ook! Ook! Ook! Ook! Ook! Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! 
Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook. Ook! Ook. 

Hello, World!:

Example for versions EsCo 0.511 (Brainfuck)

This example is Spoon translation of second Brainfuck example. Note that the intended way of coding in Spoon allows writing commands without delimiters, but current version of EsCo requires that commands are space-separated.

1111110010001011111111111101100000110100010100101111111111001000101111111111011000001101
0100101011111110010100010101110010100101111001000101111111111101100000110100010100111110
0100010000000000000011000001101000101001101101101101111100100010111110110000011010001010
0100100010101110010100000000000000000000010100000000000000000000000000010100100101001010

Hello, World!:

Example for versions EsCo 0.511 (Brainfuck)

Example for dialect Boolfuck.

H ;;;+;+;;+;+;
e +;+;+;+;;+;;+;
l ;;+;;+;+;;+;
l ;;+;;+;+;;+;
o +;;;;+;+;;+;
comma ;;+;;+;+;+;;
space ;;;;;+;+;;
W +;;;+;+;+;+;+;
o +;;;;+;+;;+;
r ;+;+;;+;;;+;
l ;;+;;+;+;;+;
d ;;+;+;;+;;+;
! +;+;;;;+;+;;
\n ;+;+;+; 

Hello, World!:

Example for versions EsCo 0.511 (Brainfuck)

This example is written in pbrain. First line defines a procedure identified with 0 which copies the contents of previous cell to the current one and adds 10 to it. Second line fills first 14 cells with values 0..130 with step 10. Finally, the values of some cells are modified to represent ASCII-codes of required characters.

(++++++++++<[>+>+<<-]>>[<<+>>-])
>::::::::::::::
<<<<<<<--------.>>>---------.+++++++..>---------.<<<<<<
<------.<--------.>>>>>---.>>>.+++.<.--------.<<<<<<<+.