The Brainfuck Programming Language
In this article we will delve a bit into programming. I am going to venture a guess that even people who are not into programming might find this interesting. The brainfuck programming language is simply so weird and mind boggling that I suspect a lot of people might be intrigued by this article.
There are a lot of different programming languages, each with their own strengths and weaknesses. Usually you will pick a programming language based on the task you wish to perform. Of course, most will have their preferred languages. Personally I tend to stick to my preferences even if it is inconvenient, some might call it insanity, I call it dedication.
Some programming languages are not created for the purpose of being practical. Instead, they aim to amuse, or possibly frustrate. These are generally referred to as esoteric programming languages
. There is one in particular that I find quite amusing. the brainfuck programming language. The best way to describe this language would have to be the word brainfuck
. The code is incredibly hard to read, and even simple programs can be a pain to write.
The language consists of only eight commands:
> increment the memory pointer (move to the right)
< decrement the memory pointer (move to the left)
+ increment the byte at the current memory pointer
- decrement the byte at the current memory pointer
. output the byte at the current memory pointer
, take one byte of input and store it in the current memory pointer
[ if the byte at the current memory pointer is 0, skip to the next corresponding ']', otherwise read the next instruction normally
] go back to corresponding '[' if the byte at the current memory pointer is 0
Overall, it's a very simple programming language, but because of that it becomes exceedingly complicated to write simple code. Just to give a simple example we will use the traditional "Hello World!" program. In certain programming languages, such a program is very easy.
Bash:
echo Hello World!
Python:
print('Hello World!')
C (this is a bit more involved):
#include <stdio.h>
int main(void)
{
printf("Hello World!");
return 0;
}
There are multiple ways to do this in brainfuck, but here's one example I got from the documentation of the Beef interpreter:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
So yeah, on first look it might appear to be utter nonsense. This is also one of the reasons why I really like brainfuck. It is not intuitive at all, but at the same time, once you go through the code you will see just how logic it is.
I am going to break up the code above into smaller sections and explain what is going on.
++++++++++[>+++++++>++++++++++>+++>+<<<<-]
This part will first set the first memory value to 10. Next the '[' character checks the value, 10, sees that it is not 0 so the loop runs. It moves to the second memory location, increments it by 7. Then the third memory location is incremented by 10. Fourth memory location is incremented by 3. Fifth location is incremented by 1. Then it returns to the first memory location is decrements it by 1. First memory location is now 9, which is still not 0, so the whole loop will run again. Essentially, memory locations 2, 3, 4, 5 are incremented by 7, 10, 3, 1. This happens exactly 10 times. So memory will look like this after the loop is run 10 times:
000 070 100 030 010
>++.
Second location is incremented to 72, the ASCII value of 'H', and printed to output.
>+.
Third location is incremented to 101, the ASCII value of 'e', and printed to output.
+++++++..
Still at the third memory location, it is incremented by 7 to the new value of 108, the ASCII value of 'l', and printed to output twice.
+++.
Third memory location is incremented further to 111, ASCII 'o', and printed to output. We now have the word 'Hello'.
>++.
We move to the fourth memory location and increment it to 32, the ASCII value for Space and print it to output.
<<+++++++++++++++.
Now we go back to the second memory location, decrement it to 87, ASCII 'W' and print it.
>.
Go forward to the third memory location, which is still value 111, ASCII 'o' and print it.
+++.
Increment the third memory location to the value 114, ASCII 'r' and print it.
------.
Decrement third memory location to 108, the ASCII value of 'l' and print it.
--------.
Further decrement the third location to 100, the ASCII value of 'd' and print it.
>+.
Go to the fourth memory location and increment it to 33, the ASCII value of '!' and print it. We now have 'Hello World!', but as is customary, we should print a 'newline' character at the end.
>.
Go to the fifth memory location, which has a value of 10. This is the ASCII value of 'newline', and print it.
This is obviously a very convoluted way to write a program, but at the other hand I find it very refreshing. In some ways, it is useful to help gain a new appreciation for how computers actually work. It gives a new perspective to programming. One very interesting example of brainfuck code would be an imitation of the cat program:
,[.,]
I really like this. It takes one byte of input, outputs it and repeats. On the more technical side it should be noted that the program above makes a certain assumption about the implementation. The program above will require that the current memory cell be set to the value of 0 upon encountering the end of input (EOF in programming terms). If, for instance the implementation does not alter the memory value upon reading EOF, then the program above will require a slight modification:
,[.[-],]
The only difference with the code above is that it will reset the memory cell to 0 just before reading the next input byte. So if it encounters the EOF, it will still exit gracefully. Once again, this is only necessary if the implementation does not edit current memory upon encountering EOF.
Additional reading
What is ASCII? Quickly summarized, it is just a definition of what byte values correspond to text characters. For a more detailed description, check out the Wikipedia article on ASCII.
If you are interested in a more in-depth read about brainfuck, or other esoteric programming languages, check out brainfuck on esolangs.
Article about my custom made interpreter, vbfi.
Categories: Programming
You need to Log in to comment.
If you don't have an account, then feel free to Create an account