DEV Community

kalium.xyz
kalium.xyz

Posted on

Brainf*ck in 5 minutes.

The easiest least readable programming language I’ve written in so far has to be Brainf*ck. Brainf*ck is what I’d call a tacobell programming language, everything is made from the same 8 ingredients. You use the 8 operators to manipulate one datapointer (a number stored somewhere) and to change datapointers (Brainf*ck has 30.000 datapointers).

The operators are as follows:
“>” moves the datapointer to the right.
“<” moves the datapointer to the left.
“+” increases the selected datapointer by one.
“-” decreases the selected datapointer by one.
“.” outputs the number in the selected datapointer as an ASCII character.
“,” reads one character from input and writes it to the datapointer.
“[” if the current datapointer is at 0, then skip to the next “]”.
“]” if the current datapointer is not at 0, then go back to the last “[”.

Take note of the last two being able to form a loop to do clever manipulations.

So now that you know the commands you want to print the text: “Hello world!” as is traditionally the first thing to do with any new programming language. You start by looking up the ASCII number for every character:
H = 72,
e = 101,
l = 108,
l = 108,
o = 111,
space = 32,
w = 119,
o = 111,
r = 114,
l = 108,
d = 100,
! = 33.

Now the first datapointer will be used as an index for the loops, the second for the characters, and a third one for the space and exclamation mark. following below is the tutorial.

First we want 72, we know that 72 = 12 * 6 so we set the index to 6:

++++++

Then we start a loop that increments our second datapointer by 12 for every point it deducts in the first datapointer (making it go up to 72 before our loop stops)

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

And then we can print the “H”

>.

Now we need to increment the second datapointer by 101 - 72 = 29, a prime number so the same trick won’t work again. We will use 28 + 1 instead.

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

We can now print the “e” and simply do +7, print again twice, +3 and print again.

>+.+++++++..+++.

Now we are going to use the third pointer, put it at 4 * 8 = 32 and print space.

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

Back to the second pointer for the rest of the letters same as before.

<++++++++.--------.+++.------.--------.>+.

Our final code will be:

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

Easily writeable and understandable.

Online brainf*ck interpretator:
https://copy.sh/brainfuck/

Top comments (3)

Collapse
 
dalmo profile image
Dalmo Mendonça

Excellent presentation, way more understandable than wikipedia's example! Thank you, sir.

I always thought brainfuck was one of those internal jokes I'd never get. After reading this, I immediately thought it'd be fun to visually watch the process... and found this fatiherikli.github.io/brainfuck-vi...

Collapse
 
iceorfiresite profile image
Ice or Fire

That makes my brain hurt

Collapse
 
silverman42 profile image
Sylvester Nkeze

😌😌. I feel high from just reading this post. My head hurts