Brainfuck – Introduction to a Esoteric Programming Language

Scroll down to content

This post is translation of my original post on the same topic in Marathi. Click Here to see original post in Marathi.

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

No, this is not a mistake. Your web browser or mobile phone app is running properly. It is written exactly as you read above. This is a short program written in a computer language. Surprised? In this article, we will introduce you to this computer language.

A computer programming language is a set of specific instructions that a human being can use to give a computer the right commands and make it do some work. There are several types of programming languages. One of them is High Level Programming Languages. The programming language is designed to be easy for people to read, and it can be easy to create commands without having to go through a lot of basic computer details and is designed for such purposes.

The set of instructions used in such languages is relatively large. You may be aware about some of these popular computer languages like C, C ++, Visual Basic, Python, Dot Net, Lisp, Ruby, Perl etc.

Sample code written in Python programming language below:

print (“Jay Maharashtra!”)

Reading the above line, you will immediately understand that it is a command to print, the phrase “Jai Maharashtra”. If you go deeper, you will notice that Python and similar computer languages are made to be easy to read and understand.

But today, in this article, we are going to introduce a computer language that is deliberately created using limited set of instructions and which are very difficult for a human to read.

The name of the language is BrainFuck. It was created by Urban Müller in 1993. Brainfuck is a kind of Esoteric programming language. The purpose behind creating Esoteric computer language is to experiment with computer language design, to come up with new ideas, to use software as a form of art, just for fun or for hacking; Or one of these. These languages ​​are not always used for commercial purposes.

Brainfuck has only eight instructions and one instruction pointer. Let us now see how to write algorithms in Brainfuck. The following is a list of algorithmic instructions in Brainfuck:

InstructionDescription
>Shift data pointer by one cell to the right.
<Shift data pointer by one cell to the left.
+Increase byte value of current data pointer, by one.
Decrese byte value of current data pointert, by one.
.Take output of byte data from current cell.
,Take input and store it in current cell.
[If byte data in current cell is zero, move instruction pointer to the pairing closing square bracket ].
]If byte data in current cell is non-zero then move instruction pointer to the pairing Opening Square bracket.

Imagine that you are writing this program on a tape.

  1. This tape can be read from left to right and back.
  2. There are cells in the tape and data can be written and read in this cell.
  3. One byte of data is stored in a cell
  4. After reading and processing current instruction, instruction pointer moves to next instruction as per the moving logic provided.

This tape may look like this at the beginning:

brainfuck_tape_1

Data pointer is shown with red color. Right now data pointer is pointing to first cell.

Initial data status in this Tape is like this:

brainfuck_tape_2

Now we will write a Brainfuck code, to sum to numbers using above information.

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

Code From Wikipedia: https://en.wikipedia.org/wiki/Brainfuck

pseudo algorithm of above code:

  1. Input number two in first cell. Input number five in second cell.
  2. Increase value of first cell and decrease value of second cell, until value is second cell is not zero. At the end of this loop, we will have sum of these two numbers stored in first cell.
  3. Using one more loop code, we will be doing similar iteration to add the number 48 into the total we got in first cell. That will give ust he ASCII compatible value of the result, which we can understand.

Lets read instructions from above code one by one to see how it affects the status of Tape. Current instruction being read is shown in red color.

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: Increase byte data in current cell by one.

brainfuck_tape_3

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: Increase byte data in current cell by one.

brainfuck_tape_4

We got number one in first cell.

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: Move data pointer to next cell on Tape.

bf5

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

After reading these five instructions one by one, we will have number five is second cell.

bf6

We got the number five is second cell.

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: If current cell data is zero, then move instruction pointer to the next instruction of the closing square bracket in this pair (which will end the loop), else move to next instruction.

But data in current cell (that is second cell) is not zero, it is number five, (a non-zero value) hence instead of moving to the next instruction of the closing square bracket, we will simply move to next instruction.

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: move data pointer to the previous cell on Tape.

bf7

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: Increase byte data in current cell by one.

bf8

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: move data pointer to next cell on Tape.

bf9

+ + > + + + + + [ < + > ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: decrease current cell byte data by one.

bf10

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: if data in current cell is non-zero, then instead of moving to next instruction, move instruction pointer to the opening square bracket of this pair.

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: move data pointer to previous cell on Tape.

bf11

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: increase data in current cell by one.

bf12

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: move data pointer to next cell on Tape.

bf13

+ + > + + + + + [ < + > ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: increase byte data by one in current cell.

bf14

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: if byte data in current cell is zero, then instead of moving to next instruction, move instruction pointer to the next instruction of the closing square bracket of this pair.

By now you would have understood that [ < + > – ] is the main loop. After iterating this loop five times, we will get the sum in first cell and at the end of this loop value is second cell will be zero, as shown below.

bf15

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: If byte data in current cell is non-zero then move instruction pointer to the pairing Opening Square bracket, else move instruction pointer to next instruction.

We are at the end of first loop which gives us the sum in first cell. Lets look into second loop which converts this sum to ASCII value. Data in current cell is zero, hence we will move instruction pointer to next cell.

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Here we have + instruction eight times. As explained in the pseudo algorith, we want to convert our sum to ASCII format and for that we need to add 48 to any number. After doing + eight times we will get number eight in current cell and after iterating the second loop first time we will get number six in second cell, since the loop has + six times. Once we execute this full loop, similar to how we executed the first loop we will get the + instruction 48 times. We created this loop because we want to avoid writing ++ instruction 48 times to convert our sum to ASCII.

After adding this number 48 to our sum, we get the number 55 in first cell and zero in second cell. In ASCII 55 means 7!

bf16

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: move data pointer to previous cell on Tape.

bf17

+ + > + + + + + [ < + > – ] + + + + + + + + [ < + + + + + + > – ] < .

Instruction: output the value in current cell. Which is the number 7 as below:

7

Now you try to decode the output of the Brainfuck code written at the start of this article.

Homework

  1. Modify above code to take input from user at run time and then sum those two numbers.
  2. Modify above code to multiple two numbers.

Enter your email address to follow this blog and receive notifications of new posts by email.

Features Image for this blog post taken from this Flickr page.

Comment

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: