You are on page 1of 5

Brainfuck

Not to be confused with Brain Fuck Scheduler. [ 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 is an esoteric programming language noted
for its extreme minimalism. The language consists of only Brainfuck programs can be translated into C using the
eight simple commands and an instruction pointer. It is following substitutions, assuming ptr is of type unsigned
designed to challenge and amuse programmers, and was char* and has been initialized to point to an array of ze-
not made to be suitable for practical use. It was created roed bytes:
in 1993 by Urban Müller. As the name suggests, brainfuck programs tend to be dif-
ficult to comprehend. This is partly because any mildly
complex task requires a long sequence of commands;
1 Language design partly it is because the program’s text gives no direct in-
dications of the program’s state. These, as well as brain-
Urban Müller created brainfuck in 1993 with the in- fuck’s inefficiency and its limited input/output capabili-
tention of designing a language which could be imple- ties, are some of the reasons it is not used for serious pro-
mented with the smallest possible compiler,[1] inspired gramming. Nonetheless, like any Turing-complete lan-
by the 1024-byte compiler for the FALSE programming guage, brainfuck is theoretically capable of computing
language.[2] Several brainfuck compilers have been made any computable function or simulating any other com-
smaller than 200 bytes. One compiler of 100 bytes is putational model, if given access to an unlimited amount
known to exist.[3] The classic distribution is Müller’s ver- of memory.[5] A variety of brainfuck programs have been
sion 2, containing a compiler for the Amiga, an inter- written.[6] Although brainfuck programs, especially com-
preter, example programs, and a readme document.[4] plicated ones, are difficult to write, it is quite trivial to
write an interpreter for brainfuck in a more typical lan-
The language consists of eight commands, listed below. guage such as C due to its simplicity. There even exists
A brainfuck program is a sequence of these commands, a brainfuck interpreter written in the brainfuck language
possibly interspersed with other characters (which are ig- itself.[7]
nored). The commands are executed sequentially, with
some exceptions: an instruction pointer begins at the
first command, and each command it points to is exe- 1.2 Brainfuck’s formal “parent language”
cuted, after which it normally moves forward to the next
command. The program terminates when the instruction Except for its two I/O commands, brainfuck is a minor
pointer moves past the last command. variation of the formal programming language P′′ cre-
ated by Corrado Böhm in 1964. In fact, using six sym-
The brainfuck language uses a simple machine model
bols equivalent to the respective brainfuck commands
consisting of the program and instruction pointer, as well
+, -, <, >, [, ], Böhm provided an explicit program for
as an array of at least 30,000 byte cells initialized to zero;
each of the basic functions that together serve to com-
a movable data pointer (initialized to point to the leftmost
pute any computable function. So in a very real sense,
byte of the array); and two streams of bytes for input and
the first “brainfuck” programs appear in Böhm’s 1964 pa-
output (most often connected to a keyboard and a monitor
per – and they were programs sufficient to prove Turing-
respectively, and using the ASCII character encoding).
completeness.

1.1 Commands
2 Examples
The eight language commands, each consisting of a single
character: 2.1 Hello World!
(Alternatively, the ] command may instead be translated
as an unconditional jump to the corresponding [ com- The following program prints “Hello World!" and a new-
mand, or vice versa; programs will behave the same but line to the screen:
will run more slowly, due to unnecessary double search- [ This program prints “Hello World!" and a newline to
ing.) the screen, its length is 106 active command characters

1
2 3 PORTABILITY ISSUES

[it is not the shortest.] This loop is a “comment loop”, Regarding the division algorithm, when dividing y by z to
it’s a simple way of adding a comment to a BF program get a quotient q and remainder r, there is an outer loop
such that you don't have to worry about any command which sets q and r first to the quotient and remainder of
characters. Any ".”, ",”, "+", "-", "<" and ">" characters 1/z, then to those of 2/z, and so on; after it has executed
are simply ignored, the "[" and "]" characters just have y times, this outer loop terminates, leaving q and r set
to be balanced. ] +++++ +++ Set Cell #0 to 8 [ >++++ to the quotient and remainder of y/z. (The dividend y
Add 4 to Cell #1; this will always set Cell #1 to 4 [ as is used as a diminishing counter that controls how many
the cell will be cleared by the loop >++ Add 2 to Cell #2 times this loop is executed.) Within the loop, there is
>+++ Add 3 to Cell #3 >+++ Add 3 to Cell #4 >+ Add 1 code to increment r and decrement y, which is usually
to Cell #5 <<<<- Decrement the loop counter in Cell #1 sufficient; however, every zth time through the outer loop,
] Loop till Cell #1 is zero; number of iterations is 4 >+ it is necessary to zero r and increment q. This is done
Add 1 to Cell #2 >+ Add 1 to Cell #3 >- Subtract 1 from with a diminishing counter set to the divisor z; each time
Cell #4 >>+ Add 1 to Cell #6 [<] Move back to the first through the outer loop, this counter is decremented, and
zero cell you find; this will be Cell #1 which was cleared when it reaches zero, it is refilled by moving the value
by the previous loop <- Decrement the loop Counter in from r back into it.
Cell #0 ] Loop till Cell #0 is zero; number of iterations is -,+[ Read first character and start outer charac-
8 The result of this is: Cell No : 0 1 2 3 4 5 6 Contents: ter reading loop -[ Skip forward if character is 0
0 0 72 104 88 32 8 Pointer : ^ >>. Cell #2 has value 72 >>++++[>++++++++<-] Set up divisor (32) for division
which is 'H' >--−. Subtract 3 from Cell #3 to get 101 loop (MEMORY LAYOUT: dividend copy remainder
which is 'e' +++++++..+++. Likewise for 'llo' from Cell divisor quotient zero zero) <+<-[ Set up dividend (x
#3 >>. Cell #5 is 32 for the space <−. Subtract 1 from minus 1) and enter division loop >+>+>-[>>>] Increase
Cell #4 for 87 to give a 'W' <. Cell #3 was set to 'o' from copy and remainder / reduce divisor / Normal case:
the end of 'Hello' +++.-----−.-------−. Cell #3 for 'rl' skip forward <[[>+<-]>>+>] Special case: move re-
and 'd' >>+. Add 1 to Cell #5 gives us an exclamation mainder back to divisor and increase quotient <<<<<-
point >++. And finally a newline from Cell #6 Decrement dividend ] End division loop ]>>>[-]+ End
skip loop; zero former divisor and reuse space for a flag
For readability, this code has been spread across many >--[-[<->+++[-]]]<[ Zero that flag unless quotient was
lines and blanks and comments have been added. Brain- 2 or 3; zero quotient; check flag ++++++++++++<[ If
fuck ignores all characters except the eight commands +- flag then set up divisor (13) for second division loop
<>[],. so no special syntax for comments is needed (as (MEMORY LAYOUT: zero copy dividend divisor
long as the comments don't contain the command char- remainder quotient zero zero) >-[>+>>] Reduce divisor;
acters). The code could just as well have been written Normal case: increase remainder >[+[<+>-]>+>>]
as: Special case: increase remainder / move it back to
divisor / increase quotient <<<<<- Decrease dividend
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>-
>>+[<]<-]>>.>--−.+++++++..+++.>>.<−.<.+++.----- ] End division loop >>[<+>-] Add remainder back to
divisor to get a useful 13 >[ Skip forward if quotient was
−.-------−.>>+.>++.
0 -[ Decrement quotient and skip forward if quotient was
1 -<<[-]>> Zero quotient and divisor if quotient was 2
]<<[<<->>-]>> Zero divisor and subtract 13 from copy
if quotient was 1 ]<<[<<+>>-] Zero divisor and add 13
2.2 ROT13 to copy if quotient was 0 ] End outer skip loop (jump
to here if ((character minus 1)/32) was not 2 or 3) <[-]
This program enciphers its input with the ROT13 cipher. Clear remainder from first division if second division
To do this, it must map characters A-M (ASCII 65-77) was skipped <.[-] Output ROT13ed character from copy
to N-Z (78-90), and vice versa. Also it must map a-m and clear it <-,+ Read next character ] End character
(97-109) to n-z (110-122) and vice versa. It must map reading loop
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 termi-
nates.
The basic approach used is as follows. Calling the in- 3 Portability issues
put character x, divide x−1 by 32, keeping quotient and
remainder. Unless the quotient is 2 or 3, just output x,
having kept a copy of it during the division. If the quo- Partly because Urban Müller did not write a thorough lan-
tient is 2 or 3, divide the remainder ((x−1) modulo 32) guage specification, the many subsequent brainfuck inter-
by 13; if the quotient here is 0, output x+13; if 1, output preters and compilers have come to use slightly different
x−13; if 2, output x. dialects of brainfuck.
3.3 End-of-line code 3

3.1 Cell size protections.

In the classic distribution, the cells are of 8-bit size


(cells are bytes), and this is still the most common size. 3.3 End-of-line code
However, to read non-textual data, a brainfuck program
may need to distinguish an end-of-file condition from Different operating systems (and sometimes different
any possible byte value; thus 16-bit cells have also been programming environments) use subtly different versions
used. Some implementations have used 32-bit cells, 64- of ASCII. The most important difference is in the code
bit cells, or bignum cells with practically unlimited range, used for the end of a line of text. MS-DOS and Mi-
but programs that use this extra range are likely to be crosoft Windows use a CRLF, i.e. a 13 followed by a
slow, since storing the value n into a cell requires Ω(n) 10, in most contexts. UNIX and its descendants (includ-
time as a cell’s value may only be changed by increment- ing Linux and Mac OS X) and Amigas use just 10, and
ing and decrementing. older Macs use just 13. It would be unfortunate if brain-
fuck programs had to be rewritten for different operating
In all these variants, the , and . commands still read
systems. Fortunately, a unified standard is easy to find.
and write data in bytes. In most of them, the cells wrap
Urban Müller’s compiler and his example programs use
around, i.e. incrementing a cell which holds its maximal
10, on both input and output; so do a large majority of
value (with the + command) will bring it to its minimal
existing brainfuck programs; and 10 is also more conve-
value and vice versa. The exceptions are implementations
nient to use than CRLF. Thus, brainfuck implementations
which are distant from the underlying hardware, imple-
should make sure that brainfuck programs that assume
mentations that use bignums, and implementations that
newline=10 will run properly; many do so, but some do
try to enforce portability.
not.
Fortunately, it is usually easy to write brainfuck pro-
This assumption is also consistent with most of the
grams that do not ever cause integer wraparound or over-
world’s sample code for C and other languages, in that
flow, and therefore don't depend on cell size. Generally
they use '\n', or 10, for their newlines. On systems that
this means avoiding increment of +255 (unsigned 8-bit
use CRLF line endings, the C standard library transpar-
wraparound), or avoiding overstepping the boundaries of
ently remaps "\n” to "\r\n” on output and "\r\n” to "\n” on
[−128, +127] (signed 8-bit wraparound) (since there are
input for streams not opened in binary mode.
no comparison operators, a program cannot distinguish
between a signed and unsigned two’s complement fixed-
bit-size cell and negativeness of numbers is a matter of
3.4 End-of-file behavior
interpretation). For more details on integer wraparound,
see the Integer overflow article.
The behavior of the ",” command when an end-of-file
condition has been encountered varies. Some implemen-
tations set the cell at the pointer to 0, some set it to the C
3.2 Array size
constant EOF (in practice this is usually −1), some leave
In the classic distribution, the array has 30,000 cells, and the cell’s value unchanged. There is no real consensus;
the pointer begins at the leftmost cell. Even more cells arguments for the three behaviors are as follows.
are needed to store things like the millionth Fibonacci Setting the cell to 0 avoids the use of negative numbers,
number, and the easiest way to make the language Turing- and makes it marginally more concise to write a loop that
complete is to make the array unlimited on the right. reads characters until EOF occurs. This is a language ex-
A few implementations extend the array to the left as tension devised by Panu Kalliokoski.
[8]

well; this is an uncommon feature, and therefore portable Setting the cell to −1 allows EOF to be distinguished
brainfuck programs do not depend on it. from any byte value (if the cells are larger than bytes),
When the pointer moves outside the bounds of the array, which is necessary for reading non-textual data; also, it is
some implementations will give an error message, some the behavior of the C translation of ",” given in Müller’s
will try to extend the array dynamically, some will not readme file. However, it is not obvious that those C trans-
notice and will produce undefined behavior, and a few lations are to be taken as normative.
will move the pointer to the opposite end of the array. Leaving the cell’s value unchanged is the behavior of Ur-
Some tradeoffs are involved: expanding the array dynam- ban Müller’s brainfuck compiler. This behavior can easily
ically to the right is the most user-friendly approach and is coexist with either of the others; for instance, a program
good for memory-hungry programs, but it carries a speed that assumes EOF=0 can set the cell to 0 before each ",”
penalty. If a fixed-size array is used it is helpful to make command, and will then work correctly on implementa-
it very large, or better yet let the user set the size. Giv- tions that do either EOF=0 or EOF="no change”. It is so
ing an error message for bounds violations is very useful easy to accommodate the “no change” behavior that any
for debugging but even that carries a speed penalty un- brainfuck programmer interested in portability should do
less it can be handled by the operating system’s memory so.
4 6 EXTERNAL LINKS

4 Derivatives [8] “Beef –". Kiyuko.org. Retrieved 2013-10-30.

[9] Morgan-Mar, David (2009-03-21). “Ook!". DM’s Eso-


Many people have created brainfuck equivalents (lan- teric Programming Languages. Retrieved 2014-06-28.
guages with commands that directly map to brainfuck) or
brainfuck derivatives (languages that extend its behavior [10] Paloque-Bergès, Camille (2009). Poétique des codes sur
or map it into new semantic territory). le réseau informatique (in French). Paris: Éditions des
archives contemporaines. p. 73. ISBN 978-2-914610-
Some examples: 70-4.

• Pi, which maps brainfuck into errors in individual


digits of Pi. 6 External links
• VerboseFuck, which looks like a traditional pro-
gramming language, only what appears as parame- • Brainfuck at DMOZ
ters or expressions are actually parts of longer com-
mands that cannot be altered.
• Ook!, which maps brainfuck’s eight commands to
two-word permutations of “Ook.”, “Ook?", and
“Ook!", jokingly designed to be “writable and read-
able by orang-utans” according to its creator, a ref-
erence to the orang-utan Librarian in the novels of
Terry Pratchett.[9][10]

However, there are also unnamed minor variants (or di-


alects), possibly formed as a result of inattention, of
which some of the more common are:

• forbidding, rather than ignoring, any non-command


characters in brainfuck programs
• introducing a comment marker which comments out
the rest of the line
• various alterations of the loop semantics, sometimes
destroying Turing completeness
• requiring a special character to mark the end of the
program

5 References
[1] “The Brainfuck Programming Language”. Muppet-
labs.com. Retrieved 2013-10-30.

[2] “Wouter’s Wiki : False Language”. Strlen.com. 2013-08-


03. Retrieved 2013-10-30.

[3] “BRAINFCK IN 100 BYTES!". Pferrie.host22.com. Re-


trieved 2013-10-30.

[4] “dev/lang/brainfuck-2.lha”. Aminet. Retrieved 2013-10-


30.

[5] “BF is Turing-complete”. Iwriteiam.nl. Retrieved 2013-


10-30.

[6] “Index of /brainfuck/bf-source/prog”. Esoteric.sange.fi.


2002-01-22. Retrieved 2013-10-30.

[7] “BF interpreter written in BF”. Iwriteiam.nl. Retrieved


2013-10-30.
5

7 Text and image sources, contributors, and licenses


7.1 Text
• Brainfuck Source: http://en.wikipedia.org/wiki/Brainfuck?oldid=642443230 Contributors: AxelBoldt, General Wesc, Bryan Derksen,
Gareth Owen, Arvindn, FvdP, Frecklefoot, Patrick, Michael Hardy, Cprompt, V, SGBailey, Tannin, Paddu, Alfio, Sam Hughes, Kricxjo,
William M. Connolley, Baylink, Cferrero, Notheruser, Darkwind, Andrewa, Nikai, IMSoP, Coren, Charles Matthews, Timwi, Wikiborg,
Dysprosia, Markhurd, Maximus Rex, E23, Furrykef, Spikey, Xyb, Betterworld, Robbot, Fredrik, Chris 73, Stewartadcock, Bkell, Wile E.
Heresiarch, Ludraman, David Gerard, Centrx, Dbenbenn, Gtrmp, Ævar Arnfjörð Bjarmason, Tsca, Jorge Stolfi, Sundar, Siroxo, Btphelps,
Neilc, Nomeata, Xmnemonic, Bug, DragonflySixtyseven, Szajd, Tietew, Dpen2000, Ta bu shi da yu, DanielCristofani, Crimmer, Jiy, Gua-
nabot, Iainscott, Wclark, Andros 1337, Ben-Arba, Triskaideka, Mjpieters, Thotypous, ZeroOne, A purple wikiuser, Bunny Angel13, Sgeo,
Danakil, Pt, Autrijus, CXI, Nickj, Leif, Vanished user sdfkjertiwoi1212u5mcake, Cmdrjameson, Dungodung, Andrewbadr, Jumbuck, Tra,
Prino, M7, Water Bottle, BodyTag, Kmill, Darco, Sedimin, Ranok, Harej, Tony Sidaway, RainbowOfLight, VoluntarySlave, Adrian.benko,
Scottbell, Postrach, Feezo, Smoothhenry, Jeffrey O. Gustafson, RHaworth, Shreevatsa, Yuubinbako, MattGiuca, Ruud Koot, BrianV,
Optichan, Waldir, Allen3, Tslocum, Graham87, Magister Mathematicae, JIP, Grammarbot, Mendaliv, Icey, Search4Lancer, Rjwilmsi,
Koavf, Miserlou, Flarn2006, Amrik, Spankthecrumpet, Fragglet, Lambyuk, Athas, Chobot, Stoive, Bgwhite, Kakurady, YurikBot, An-
gus Lepper, Nmondal, John Quincy Adding Machine, Taejo, Splash, IByte, Bovineone, Wimt, -OOPSIE-, Trovatore, R.e.s., Ngorongoro,
Haikz, Kyle Barbour, Mistercow, Mike92591, Robost, Cedar101, 404notfound, Tom Morris, Veinor, SmackBot, Haza-w, InverseHyper-
cube, McGeddon, Matveims, Stifle, Monz, CapitalSasha, Saros136, Chris the speller, Bluebot, Allard Mosk, Apankrat, Master of Pup-
pets, Mdwh, Octahedron80, Gracenotes, Yanksox, Tamfang, N.MacInnes, Onorem, Michael.Pohoreski, Benjamin Mako Hill, Cybercobra,
Daler, Derek R Bullamore, Acdx, Ugur Basak Bot, Doug Bell, TomasS, Plaicy, Chrisandtaund, Writtenonsand, Scientizzle, Ampersand777,
Scetoaux, Ivucica, Soulkeeper, SQGibbon, Etherealflaim, GDallimore, Tawkerbot2, FatalError, CRGreathouse, CBM, Neonleonb, Safalra,
Simeon, Revolus, Alucard (Dr.), Msnicki, Inkington, Protious, Jamaica mon, Jojan, Semiclear, AntiVandalBot, Ais523, Widefox, Joaquin-
Ferrero, Behco, Bridgeplayer, Aspensti, Sejomagno, Richardw, Reallybored999, IanOsgood, Martinkunev, Penubag, Magioladitis, A12n,
Charliesome, Toomai Glittershine, Tomcs, Minimiscience, DanielBeaver, Gwern, PowPit, STBot, J.delanoy, Extransit, BBrucker2, Sparsi-
tyProblem, Tarotcards, AntiSpamBot, CardinalDan, Geosultan4, VolkovBot, Jeff G., Happypal, JuneGloom07, Mr richard t king, Rowfilter,
Montyphy, Don4of4, Aaron Rotenberg, Agyle, Triesault, Nonename, Austriacus, SieBot, TJRC, Malcolmxl5, Dbelange, Mwwhtied, X-Fi6,
Revent, Pi is 3.14159, James.Denholm, Snipe.her, Cyfal, Autoplombe, ClueBot, Kanhef, Timberframe, Replysixty, NastyLucas, Bobcat43,
LinEagle, InternetMeme, XLinkBot, Rror, Khunglongcon, Speeddymon, John Hitchins, Dsimic, Jafeluv, Protonk, Chamal N, Mch-aoeu,
Geoffloh, NightmareZ, Tide rolls, OlEnglish, Zorrobot, Legobot, Yobot, Pcap, AnomieBOT, DemocraticLuntz, ArthurBot, LilHelpa, Ex-
plorer09, Ute in DC, Pegasus Epsilon, Smallman12q, Ehird, Samwb123, Flaming Idiot, Pinethicket, Jonathan Queiroz, Slavomir.dvorsky, Σ,
FireChao, Agrasa, Ivanvector, EmausBot, Cogiati, Fæ, Thargor Orlando, Anir1uph, Upiicsa, Kuashio, Colejohnson66, Ebehn, V6ak, Clue-
Bot NG, MoodyCoD, Hundredsofthousands, Rinaku, O.Koslowski, Widr, Danim, Tanzinul.islam, 2pem, Roberticus, TCN7JM, Patek68,
Sn1per, Klilidiplomus, BattyBot, RichardMills65, The Quirky Kitty, MCaecilius, Ekips39, Lethosor, Advocatejake, Fatiherikli, Esolanger,
Caedus94, Mohitgarg90, WikiChief108, JmgCID, Ciuak studio, Liamraystanley and Anonymous: 387

7.2 Images
• File:Ambox_important.svg Source: http://upload.wikimedia.org/wikipedia/commons/b/b4/Ambox_important.svg License: Public do-
main Contributors: Own work, based off of Image:Ambox scales.svg Original artist: Dsmurat (talk · contribs)
• File:Question_book-new.svg Source: http://upload.wikimedia.org/wikipedia/en/9/99/Question_book-new.svg License: Cc-by-sa-3.0
Contributors:
Created from scratch in Adobe Illustrator. Based on Image:Question book.png created by User:Equazcion Original artist:
Tkgd2007
• File:Text_document_with_red_question_mark.svg Source: http://upload.wikimedia.org/wikipedia/commons/a/a4/Text_document_
with_red_question_mark.svg License: Public domain Contributors: Created by bdesham with Inkscape; based upon Text-x-generic.svg
from the Tango project. Original artist: Benjamin D. Esham (bdesham)

7.3 Content license


• Creative Commons Attribution-Share Alike 3.0

You might also like