Either the coolest or the stupidest programming language in the world, brainfuck was designed by Urban Müller in order to create the world’s smallest compiler of a Turing-complete programming language. Originally his compiler was 240 bytes in size, but he reportedly got it down to about 200 bytes. Others have gotten it below that. The language consists of only 8 operations, which I will go into after the jump.
Operations:
- > increment the current pointer
- < decrement the current pointer
- + increment the byte at the current pointer
- - decrement the byte at the current pointer
- . output the byte at the current pointer
- , input a byte and store it at the current pointer
- [ jump forward past the matching ] if the byte at the current pointer is zero
- ] jump backward to the matching [ unless the byte at the current pointer is zero
So the fun part is coming up with the smallest program possible written in brainf*** for a given task. For example, you can write a Hello World program like so:
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-----------
----------------------------------------------------------------
----.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++
+++++++++++++++++++++.+++.------.--------.
This increments or decrements a pointer to the ASCII value for each character and prints the output. The first string of +'s increments the byte at the pointer until it reaches the ASCII value for 'H' and then outputs it. Successive increments and decrements produce the rest of the characters. Alternatively, you can create temporary variables for the pointers to the ASCII characters and then use those to print the output. This results in a much smaller implementation than the original.
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
+.<<+++++++++++++++.>.+++.------.--------.
Unfortunately, brainfrack isn’t sufficient to be a fully functioning programming language for actual use. There are no facilities for I/O beyond stdin and stdout. So any file manipulation is done by redirecting stdin and stdout. You can’t do any sorts of graphics or interact with ports. But it’s a fun toy with which you can make some of the most mind-boggling code imaginable.
Related stuff:
- brainfuck for Windows at the brainfuck archive
- brainfuck resources from the Open Directory Project




1 comment
Comments feed for this article
3 December 2007 at 10:17:04
brainfscking set theory « The Mendicant Bug
[...] computer science, math, mathematics, set theory I mentioned the esoteric programming language brainfuck a little while back. It consists of 8 operations and was created in order to make the smallest [...]