An introduction to Brainfuck programming
What can you do with a programming language with only eight commands and a pointer?
Brainfuck is an esoteric programming language created in 1993 by Urban Müller.
It is fully Turing-complete, so it is not intended for practical use, but as challenge and amusement for programmers.
Brainfuck is represented by an array with 30,000 cells initialized to zero, eight commands and a data pointer that points at the current cell.
The pointer can be moved about in the array and the current cell can be incremented or decremented, by one.
The eight commands
> →increment the data pointer (to point to the next cell to the right).
< →decrement the data pointer (to point to the next cell to the left).
+ →increment (increase by one) the byte at the data pointer.
- →decrement (decrease by one) the byte at the data pointer.
. → output the byte at the data pointer.
, → accept one byte of input, storing its value in the byte at the data pointer.
[ → If the value at the current cell is zero, skips to the corresponding ] . Otherwise, move to the next instruction.
] → If the value at the current cell is zero, move to the next instruction. Otherwise, move backwards in the instructions to the corresponding [ .
! → if the exclaim box is checked, allows the interpreter to use all characters to the right of the ! as program input.
The first program
I take as example a simple program that was written by Prajit Ramachandran on https://learnxinyminutes.com/docs/brainfuck/: it simply displays the letter ‘A’.
++++++ [ > ++++++++++ < — ] > +++++ .
First, it increments cell #1 to 6.
Cell #1 will be used for looping.
Then, it enters the loop ([) and moves to cell #2.
It increments cell #2 10 times, moves back to cell #1, and
decrements cell #1.
This loop happens 6 times (it takes 6 decrements for cell #1 to reach 0, at which point it skips to the corresponding ] and
At this point, we’re on cell #1, which has a value of 0, while cell #2 has a value of 60.
We move on cell #2, increment 5 times, for a value of 65, and then print cell #2’s value.
65 is ‘A’ in ASCII, so ‘A’ is printed to the terminal.
You can try Brainfuck on your browser with brainfuck-visualizer.