Printing “GEEKS FOR GEEKS” In Brainfuck

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.
1. > increment the data pointer (to point to the next cell to the right).
2. < decrement the data pointer (to point to the next cell to the left).
3. + increment (increase by one) the byte at the data pointer.
4. decrement (decrease by one) the byte at the data pointer.
5. . output the byte at the data pointer.
6. , accept one byte of input, storing its value in the byte at the data pointer.
7. [ 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.
8. ] 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.
 `// You need to use https://copy.sh/brainfuck/ to run  ``// this code. `` ` `/** Using different cells to store different numeric values ``  ``* Cell 1 = 70; Cell 2 = 80; Cell 3 = 30 ``  ``* These base values can then be incremented or decremented ``  ``* to store ASCII values of the characters we need to print */``++++++++++ ``//Cell 1 = 70(To run the loop 10 times) ``[ ``>+++++++ ``// Cell 1 = 7*10  ``>++++++++ ``// Cell 2 = 8*10 ``>+++ ``// Cell 3 = 3*10 ``<<<- ``// Setting the pointer back to cell 0 & decrement ``] ``// end of loop `` ` `/**Final numeric values in each cell ``   ``*Cell 0 = 0; Cell 1 = 70; Cell 2 = 80; Cell 3 = 30*/`` ` `// Moving from Cell 0 to Cell 1 ``>+.--..++++++. ``// Printing "GEEK"(ASCII 71, 69, 69 75) `` ` `// Moving from Cell 1 to Cell 2 ``>+++. ``// Printing "S"(ASCII 83) `` ` `// Moving from Cell 2 to Cell 3 ``>++. ``// Printing " "(ASCII 32) `` ` `// Moving the pointer back to Cell 1 ``<<-----. ``// Printing "F"(ASCII 70) `` ` `//Moving from Cell 1 to Cell 2 ``>----.+++. ``// Printing "OR"(ASCII 79, 82) `` ` `// Moving from Cell 2 to Cell 3 ``>. ``// Printing " "(ASCII 32) `` ` `//Moving from Cell 3 to Cell 1 ``<<+.--..++++++. ``//Printing "GEEK"(ASCII 71, 69, 69 75) `` ` `//Moving from Cell 1 to Cell 2 ``>+. ``//Printing "S"(ASCII 83) `

```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. Problems to practice : 1) BGEEK 2) BFMUL Sources: Brainfuck-Wikiwand E-mail ID: npalash25@gmail.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.

Previous
Next