From WikiChip
Brainfuck

Brainfuck (also bf) is an esoteric programming language developed in 1993 by Swiss Physicist Urban Müller. The language get its name not from being abnormally complicated but rather for being unusual and unique, seeing that the language is made up of nothing more than eight simple commands and an instruction pointer. Brainfuck was designed to have the smallest possible compiler in mind with several compilers built with under 200 bytes.

Design and Operation[edit]

The basis of Brainfuck revolves around the traversal of an array of at least 30000, 8-bit, integer elements, although bitwidth conversions may expand a normal brainfuck program to 16-bit or 32-bit. Each element in the array, known as a 'cell', is initially set to zero. The value of each cell in the program may be manipulated using the operators below. The program is run sequentially and all characters beside the operators listed below are ignored.


Character Meaning
> Increment Data Pointer
< Decrement Data Pointer
+ Increment Byte of Data Pointer
- Decrement Byte of Data Pointer
, Accept one byte of input
. Output value of Data Pointer
[ If nonzero value, execute command in square brackets '[ ]'.
] If nonzero value, execute command after corresponding '[' bracket.

Brainfuck Operators[edit]

The '<' and '>' operators simply move the data pointer to the element to the left '<' or to the right '>' of where the pointer was before the operation is executed. The Data Pointer is initially pointing to the first element '0' of the array and therefore can't be moved to the left using the '<' operator.

The '+' and '-' operators increment and decrement the value of the byte cell by one such that the cell may take any 8-bit value (0-255). One can use the value of a cell in place of it's ASCII counterpart and can display the ASCII character by using the Output Operator '.'. An example of this would be using the '.' operator to display to the output the ASCII character 'A' when the value of the cell currently being pointed to by the data pointer is '65'.

The '.' operator, as previously stated, converts the value of the cell that is currently being pointed to by the data pointer to ASCII which is subsequently displayed to the console.

The Input ',' Operator accepts one character of user input and replaces the value of the cell that the data pointer is pointing at with the ASCII value of the inputted character. An example of this operator can be found below.

The '[' and ']' operators work similarly to brackets in other languages such that they may be used to group code together. The rule of thumb with both the '[' and ']' brackets is that the contents of the square brackets will always be ignored as long as the value of the cell that the data pointer is pointing at is zero. If the value of the cell is nonzero when the '[' operator is the next command, the contents in that bracket will be executed. If the value of the cell is nonzero when the ']' is the next command, the contents of the square bracket will be run again.