The Puzzle of BrainFuck: Beyond the Ordinary
You probably be wondering what the hell is brainfuck ๐, this is only an esoteric programming language that was created in 1993 by Urban Mรผller. The language was created to test the boundaries of programming language design and to explain key concepts in another language. The word "esoteric" distinguishes it from languages developers use to write software for systems.
So what is BrainFuck?
Brainfuck is an extremely minimalistic language that makes use of only eight simple commands, a data pointer and an instruction pointer. Despite its simplicity, writing programs in Brainfuck can be quite challenging due to its lack of conventional control structures and the need to manipulate memory directly.
BrainFuck Commands
The program operates on an array of memory cells, each initially set to zero. The commands manipulate the current memory cell the pointer is pointing to. Below are the commands used in brainfuck language:
COMMANDS | FUNCTION |
< - The less than sign | Moves the memory pointer to the left |
> - The greater than sign | Moves the memory pointer to the right |
+ - The plus sign | Increases the current cell by 1 |
- - The minus sign | Decreases the current cell by 1 |
, - comma | Reads a single character and stores it in a memory cell (ASCII value of character is stored) |
[ - The left square bracket | Jump past the corresponding ] if the value at the current cell is 0. |
] - The right square bracket | Jump back to the corresponding [ if the value at the current cell is nonzero. |
. - Period | Output the ASCII value at the current cell. |
Cell Representation of Data
All memory cells are initialized to zero, the interpreter points to the first cell which from there we can move to any cell we want using the simple commands of the language.
The <
and >
commands are used to move back and forth for each memory cell of the brainfuck array. The size of each memory cell in Brainfuck is not strictly defined by the language specification. It's up to the implementation and interpreter. In most cases, each memory cell is designed to hold a single byte, allowing for values in the range of 0 to 255 (assuming an 8-bit byte).
However, some Brainfuck interpreters might use a different representation, such as 32-bit cells or even larger. The basic concept is that each cell represents a single piece of memory, and the interpreter operates on the values within these cells. This is why it is a good practice to first read the documentation of the interpreter.
>>>
- This will move the pointer three times to the memory cell with index 3
++++
- In the current cell where the memory pointer is, the cell value will be increased to the number of+
passed to the interpreter.
Similar to -
command, the command will decrease the current memory cell by 1, so the current cell will be 3 after the command is executed.
.
- This will output the ASCII value at the current cell, let's say the value in the current cell is 69, so the ASCII value to will be printed in standard output is E.,
- This symbol is for storing a single character in the current memory cell. This will wait for the user's character input to be stored in the memory cell
[
]
- These commands are used to create a simple loop with the values of the memory cell. To better understand this, let us consider a simple example:
>++[>++<-]
;
The Brainfuck code >++[>++<-]
performs the following operations:
>
: Move to the second memory cell.++
: Increment the value at the current memory cell (the second cell) by 2.[
: Start a loop that continues while the value at the current memory cell (second cell) is not zero.>++<-
: Within the loop:>
: Move to the next memory cell.++
: Increment the value at the current memory cell (the third cell) by 2.<
: Move back to the second memory cell.-
: Decrement the value at the second memory cell.
This loop effectively multiplies the value at the third cell by 2 and decrements the value at the second cell until it becomes zero.
]
: Close the loop. The loop will repeat until the value at the second memory cell is zero.You can add commands at the end i.e.
>++[>++<-]>.
, this will print what 4 represents in the ASCII table after the loop is over (It will print nothing because 4 represents End of TRANSMISSION in the ASCII table)
GETTING STARTED WITH BRAINFUCK LANGUAGE
With a good understanding of how to use brainfuck language, it commands and how memory cells are given data, let us look at how to install the interpreter. We could write brainfuck codes but where are going to run it and what is the file extension for brainfuck programs.
The extension of brainfuck programs is .bf
.
Installing the brainfuck interpreter will depend on your operating system;
Write this code in your computer's terminal;
Windows:
Using Chocolatey:
If you have Chocolatey installed, you can use it to install a Brainfuck interpreter.
Open a Command Prompt as Administrator and run:
choco install brainfuck
Alternatively, you can run your code in a virtual environment interpreter directly from here.
Linux:
Using Package Manager (Debian/Ubuntu):
Open a terminal and run:
sudo apt-get update sudo apt-get install bf
macOS:
Using Homebrew:
If you have Homebrew installed, open a terminal and run:
brew install brainfuck
After installing or downloading the interpreter, you can use it by opening a terminal or Command Prompt and running the bf
command followed by the path to your Brainfuck code file.
bf <file-path.bf>
Your brainfuck code should run perfectly well. ๐
For further studies of the brainfuck language history and concepts, I recommend this article.
Subscribe to my newsletter
Read articles from Wiseman Umanah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Wiseman Umanah
Wiseman Umanah
I'm WIseman Umanah, a developer ๐ป๏ธ who loves to share my knowledge and experiences with others and I'm always looking for ways to improve my skills and learn new things. When I'm not working, you can find me drawing as an artist ๐ค๐.