Brainfuck
Brainfuck is a minimalistic programming language invented by Urban Mueller. It consists of eight different commands, which are represented by single characters (see the table below) and an array with enough space [tm].
The array needs to have infinite elements for exact Turing-completeness. As this is not possible currently, standard implementations of Brainfuck store 30000 bytes, which is enough for most algorithms on the net.
The interesting thing with Brainfuck is that you can do everything you can imagine using these eight commands. It shows the simplicity of todays digital world in a very amusing way: As the language itself is extremely hard to write (and read), people are shocked by its complexity, while it proves that everything computers are able to do can be represented using only eight simple instructions.
I have written an easy-to-understand
Brainfuck interpreter in C, so you can start immediately. It includes an
additional debugging operator (#) to print out the decimal number that is
stored at the current position in the array (instead of the corresponding
ASCII character).
I also wrote a tiny and obfuscated
version (344 bytes).
At the beginning, the value of every field in the array is 0 and the pointer points to the first location.
- >
- Move pointer right.
- <
- Move pointer left.
- +
- Increment the value at the pointer.
- -
- Decrement the value at the pointer.
- [
- If the value at the pointer is 0, jump to matching ].
- ]
- If the value at the pointer is not 0, jump to matching [.
- ,
- Read a character and save it at the current position in the buffer. (e.g. 'A'=65)
- .
- Print the value at the current position as an ASCII character.
All the characters not listed above are considered as comments and ignored by the interpreter/compiler.
Understanding the power of the built-in loop operators ('[' and ']') is an essential task for Brainfuck beginners, as they allow almost everything you can imagine. Have a look at The Brainfuck Archive, if you want to see some really amazing snippets of code.
Now I want to give you some impressions of the language.
Store numbers
+++++++>+++
Store 7 in the first position of the array and 3 in the second one.
Addition
A>B[<+>-]<
Calculate A+B, where A and B stand for any positive integers. Please keep in mind that it is not possible to use symbolic names in Brainfuck. If you want to do an addition in your code, you have to replace A and B with some numerical values as in the first example.
Multiplication
>A[<B>-]<
Calculate A*B. At first, we write A to the second position in the array. Then, we add B to the first position and decrement A. This is repeated, as long as A is >0.
In C, this would look like this:
pos++;
p[pos] += A;
while(p[pos]){
pos--;
p[pos] += B;
pos++
p[pos]--;
}
pos--;
Print Hello World!
The easiest way of writing a Brainfuck program that prints
Hello World
is to first calculate the ASCII code of 'H' (72),
print this character, go to the next position in the array, calculate
an 'e' (101), print this character, and so on.
The problem with this approach is that you waste cpu time,
because you have to do redundant work. Therefore, I wrote an
improved version that doesn't calculate the characters beginning
from 0, but rather by reusing the character printed before.
++++++++++ [>+++++++>+<<-]>++. H >[<++>>+<-]<+++++++++. e +++++++.. ll +++. o >>[<+++>-]<++. SPACE [>++<-]++[>+++++++++++<-]>+. W <<. o +++. r ------. l --------. d >+++++++[>--------<-]>++. !
At first, we multiply 10*7 and add 2 (so we have 72, 'H'). Then, we add 29 (72+2*10+9=101, 'e'), add 7 (101+7=108, 'l'), etc.
Got Brainfucked? ;)
Last change: Tue, 04 Nov 2008 00:58:13 +0100
+++++++++>+++++[<->-]
The actual subtraction takes place in the loop. The commands before initialize the operands we want to use (9 and 5).