A few months back while looking at the HackerEarth developer profile of one of my seniors in college I discovered that one of the languages in his language activity included BRAINFUCK. This piqued my interest so I googled it and to my surprise I did not find a single link to an article on GeeksForGeeks that was on brainfuck. So anyway I read a little about it on Wikiwand & LearnXinYMinutes & after that I solved my first problem on SPOJ which gave me 0.6 points and boosted my global rank from 44000 to 22296.
Ever since then I wanted to write an article about brainfuck on GeeksForGeeks since it is the first website that students refer to for any programming related articles.
Since it is a minimalist language writing code in it can be pretty lengthy and you have to meticulously keep track of every statement, operations & characters that you type. It tests your patience as a programmer and is absolutely frustrating(hence its name), but when you get that accepted tick mark it is one of the most satisfying feeling a programmer can experience.
Before we start with the actual code let’s have a look at some of its features =>
- It is an esoteric programming language(Esolang)
- It consists of only 8 commands that are listed below.
- > increment the data pointer (to point to the next cell to the right).
- < decrement the data pointer (to point to the 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 byte at the data pointer.
- , accept one byte of input, storing its value in the byte at the data pointer.
- [ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
- ] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
- Brainfuck stores data in the form of cells of 8-bit size(cells are bytes).
- In the classic distribution, the array of cells has 30,000 cells, and the pointer begins at the leftmost cell(Cell 0).
To put our newly acquired knowledge of brainfuck into practice we are going to write a simple program to print GEEKS FOR GEEKS.
The approach is simple. First we need to list the types of characters that we want to print and list their ASCII codes. Then we need to store the values nearest to their ASCII codes in the required number of cells so that we can get the exact ASCII codes by incrementing or decrementing those cell values and print the required characters.
Before scrolling down further and seeing the code I strongly suggest that you try to write the code on your own. You can test your code on this IDE – Le Brainfuck specifically for compiling BrainFuck code.
Output : GEEKS FOR GEEKS
Applications of brainfuck:
1) Brainfuck does not have a lot of practical uses.
2) It’s creator Urban Müller designed brainfuck with the goal of implementing it with the smallest possible compiler.
3) Esoteric programming languages are usually used to test the boundaries of computer programming language design, as software art or just for recreational programming.
If you want to take a look at some more programs written in brainfuck got to this link.
This article is contributed by Palash Nigam.
E-mail ID: email@example.com
Please write comments if you find anything incorrect or if you have a shorter version of the above code, or you want to share more information about the topic discussed above.