Brainfuck Coding Language

This language appeared in 1993 and is designed to challenge and amuse programmers. Not for practical use though. This console-based language only works with and displays integers and strings, it has no power whatsoever.

Check out the Hello World! script in Brainfuck:

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

The language is very easy to learn but hard to work with, there are only eight commands:

=================================================================================
>             Increment data pointer (point to next cell to the right)
<             Decrement data pointer (point to next cell to the left)
+             Increment (increase by one) the byte at the data pointer
-             Decrement (decrease by one) the byte at the data pointer
.             Output the ASCII Decimal CharCode value at the data pointer
,             Accept one byte of input, storing its value in the byte at the data pointer
[             If byte at data pointer is 0, execution jumps to command after matching ]
]             If byte at data pointer is non-0, execution jumps to command after matching [
=================================================================================

Source: http://en.wikipedia.org/wiki/Brainfuck

For the hello world above this is how it works:

First you have 10 + signs that add ten bytes to cell[0]. Then it goes into a loop which is surrounded by [ and ] signs, this keeps looping until the value of bytes within the current cell becomes 0 (Right now our current cell is cell[0]). In the loop it moves to the next cell to the right (cell[1]) and adds 7 bytes to this cell. Then another > sign to move to the next cell to the right (cell[2]) and then it adds 10 bytes to this cell. It does the same thing adding 3 bytes to cell[3] and 1 byte to cell[4]. Next it moves 4 times to the left back to cell[0] and takes away one byte from this cells value, this is where it loops afterward since the current cell (cell[0]) is not equal to 0 bytes it will keep looping to [ until the current cell contains 0 bytes. Since cell[0] originally had 10 bytes the loop will loop 10 times which means after the loop cell[0] = 0, cell[1] = 70, cell[2] = 100, cell[3] = 30, cell[4] = 10.
Here’s where it starts making sense: Now after the loop we should be on cell[0] so now it moves to cell[1], adds 2 bytes to it and uses a period (.) to display the ascii value of the current cell; in this case it is 72: in ascii it is the decimal character code for “H” therefore it outputs “H” to the console. Now it moves forward to cell[2], adds 1 to it and displays chr(101) which is “e”. Next it adds 7 to the current cell and displays it two times outputting chr(108) “ll” (these are L’s). Add 3 and display chr(111). And it should keep doing this to spell out hello world with ascii decimal codes. And finally at the end it prints the ascii code 10 which is the text termination.

Get a brainfuck compiler: http://home.arcor.de/partusch/html_en/bfd.html
Online brainfuck interpreter: http://brainfuck.tk/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: