Skip to main content

Command Palette

Search for a command to run...

Brainfuck - The Programming Language

Published
3 min read
Brainfuck - The Programming Language

“How minimalistic can a programming language be?”. Can it be as minimalistic as Brainfuck, an esoteric (mysterious, hard to understand) programming language created in 1993. Its creator Urban Muller set out for its creation with an aim of implementing the smallest possible compiler; he succeeded with a size of 296 bytes.

So, how does this esoteric programming language work?

Well, the language consists of 8 commands listed below:

The commands are executed sequentially and hence the program is a sequence of these commands. Any character other than the eight specified above should be disregarded by the compiler or interpreter. Characters other than the eight operators should be considered comments. As the name implies, Brainfuck programmes are sometimes tough to understand. It is essentially a Turing-complete language.

The idea behind Brainfuck is memory manipulation. Essentially, we are provided an array of 30,000 1-byte memory chunks. The array size is really determined by the implementation used in the compiler or interpreter, although normal brainfuck is 30,000. Within this array, you may raise the memory pointer, the memory pointer's value, and so forth.

As roachhd mentions in his GitHub post : “When cavemen started scrawling shit on the walls, the first ever picture drawn was a man waving at a picture of the planet earth.” So why not look at “Hello World” through the eyes of Brainfuck :

Hello World

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

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

+++++++..

+++.

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

------------.

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

<.

+++.

------.

--------.

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

I'm sure this made you go

Here is a brief explanation:

The first line begins with a “>” followed by 8 “+” which implies moving one cell to the right and incrementing that cell’s value eight times. This gives us—you guessed it—the value 8 in cell 1. Following this is a “[“, then a “<” and a total of 9 “+” which is followed by a “>”, a “-” and a closing “]”. Thus, we begin a loop( [ ) in which we move left (<,back to cell 0), add nine (9+s) to that cell, move right again(>), and remove one from the number in cell 1. Remember that the loop runs until the cell value reaches zero, thus this would be repeated eight times, each time adding nine to cell 0. So we can see that cell 0 has a value of 72 at this moment, which corresponds to the capital letter 'H' in the ASCII table. The line is concluded by a “.” which corresponds to the output of the character “H”.

memory blocks

A memory block visualization yields:

-------------

[72][0][0][0][0][0]...

^

memory pointer

Similarly in the second line we have added the ASCII value of 101 that represents “e”.

In this way we can interpret this language that stands true to its name.

As can be understood, the language scores very low in its efficiency. However it is still a cult classic among programmers of various age groups. Use at your own risk and get ready to be “Brainf#ck”-ed.