Brain****

0 Conversations

One of the best-known and most practised esolangs is brain****1, designed by Urban Müller in 1993. He wanted to create a Turing-complete language with a compiler as small as possible - the current minimum size is 98 Bytes.

Brain**** consists of only eight commands, each represented by a single character, and works much like a Turing Machine, with a few differences. Imagine a strip of tape divided in cells, each of them containing a number. A pointer points to a certain cell and may increase or decrease its content by one. Furthermore a cell value can be put in or out, and a simple if-then-construct is available.

CommandMeaning
>Move the pointer to the right
<Move the pointer to the left
+Increment the cell's value
-Decrement the cell's value
.Output the cell's ASCIIfied value.
,Store an input value
[Jump to the next ] if the cell's value is zero
]Jump to the previous [ if the cell's value is not zero

Basic constructs

Input

When a programme is executed, most brainfuck interpreters will ask for a string of input. Upon the first ","-command, the character will be saved in the current cell. An internal counter gets incremented so that the next "," reads the second character, and so on. If there is no further input, the cell will be regarded as empty.

For Loops

A for loop requires a cell with the number of iterations. For example, if we want a character printed out ten times, we can use the following code:

,

> +++++ +++++

[- <.>]

First, the input character is saved in cell 1 (line 1). The pointer moves then to cell 2, which is set to the value 10 (line 2). In line 3, the loop begins: After decrementing cell 2, which holds the number of loop iterations, the pointer moves to cell 1, outputs the character in it and goes back to cell 2. The "]"-command checks whether it equals zero. If so, the programme is terminated.

Moving a byte

Moving a cell's content is quite simple with our knowledge of loops. Look at this example:

>[-]<

[->+<]

Let the pointer point to cell 1, which holds the value we want to move. In line 1, it moves to cell 2, which is cleared be decrementing it repeatedly until it equals zero. Then (line 2) cell 1 is cleared in the same manner, but in every iteration cell 2 is also incremented.

This procedure's handicap is that cell 1 gets cleared. If we want the value not to be moved but copied, we simply can move it to two cells simultaneously:

>[-]

>[-]<<

[->+>+<<]

Arithmetics


Brain**** arithmetics are quite hard stuff, since only single-digit integers are accepted as input. Thus, the input has first to be deASCIIfied and converted in proper numbers brain**** can work with in the second step, where the actual calculation is done. The result has to be converted back into its numerals, which must be ASCIIfied before given back.

Input

This procedure takes two digits a and b as input and transform them into the decimal number ab.

+++++ +++++ +++++ +++++ +++++ +++++ +++++ +++++ +++++ +++ >

,<

[-<+>>-<]

<

[->+<]

>>

>,<<

[->>-<<]

>>

[->+<]

<

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

Still not given up? Well, this entry is not over yet.

In the following section, (x) denotes cell x.

The first line is simple: (0) is set to 48. This could be done faster using a loop that adds six times the value of 8 or the other way round, but we do not want to make things even more complicated than they are. We proceed to (1), store the first byte of input in (1) and go back to (0).

48 is an important constant in brain****. Remember that input numbers are saved as their ASCII equivalents, which range from 48 (which is 0) to 57 (which is 9). By subtracting 48, we get the numbers we want. This is what the loops in lines 3-6 do: They move 48 from (0) to (-1) to save it for later use while subtracting it from the value in (1). In line 7, the second digit is stored in (2). Again, 48 is subtracted in line 8. This time it does not need to be stored, since we won't use it again.

In line 10, (2), now holding the second digit of the input number, is added to (3).

In line 12, (1), now holding the first digit of the input number, is added ten times to (3) so that (3) holds the number we want.

The code above will be referred to as "(A)" in the following sections.

The actual calculation

In this entry we will use the simple example of addition. Anyone who is familiar with assembler code won't find it difficult to do multiplication or division in brainfuck, maybe more. There are much simpler brain**** programmes

The trick is that we need not only one but two values two add them.

(A)

<

(A)

The first (A) starts in (0) end ends in (1). Luckily, (-1) to (2) are already empty so that we do not need to reset them to zero. The second (A) starts in (-1) so that (-2) to (2) are used and (3), which holds one of the summands, remains unaffected. The second summand is stored in (2). The pointer highlights (0), and all cells but (2) and (3) are empty.

>>

[->+<]

You should be familiar with the loop in line 2 by now. (3) holds our sum, all other cells equal zero and the pointer is at (2).

Conversion to output characters

First we need to divide our two-digit sum into its numerals, which have then to be converted to the corresponding ASCII characters by adding 48. The actual algorithm is very complex and far outside the scope of this entry.

Turing-Completeness

Whether brain**** is Turing-complete depends on its implementation. It is simple to prove that it is, if an infinite number of cells can hold values of infinite size. Apart from that it has also been proven that an infinite number of cells with finite size as well as a finite number of cells holding values of infinite size are enough to provide Turing-completeness. Both being finite causes the language to behave like a finite-state machine like any real computer.

Technical Details

A finite row of cells will cause errors when the pointer moves outside the ends. To prevent that, most brain**** interpreters arrange the cells in a circle so that a "<"-command makes the pointer moving to the rightmost cell when executing it in the leftmost cell and the other way round. Integers growing too big or becoming negative behave the same way. Furthermore, brain**** interpreters do not handle very large numbers. Instead, they just know values from 0 to 255, since greater integers are not assigned to ASCII characters.

Related esolangs

Some programmers became soon uncontent with the aesthetics of brain**** code and replaced the command symbols by others. Examples are Ook!, which enables Orangutans to write programmes and consists of the same eight commands found in brain****, represented by different combinations of exclamations like "Ook.", "Ook!" and "Ook?"; and COW, which does the same for bovines. Spoon is an attempt to make the already hardly readable2 brain**** code even more obfuscated by replacing the command symbols with binary numbers

There are some brainfuck implementations that provide additional commands or the possibility to call subroutines. The brain**** programmers' community regards them as sacrilege since these little extras contravene all the principles of simplicity that renders brain**** unique.

1Usually not capitalised.2To illustrate this: The "Hello World!"-example in the English wikipedia's brain**** article printed a rather obscene message about the reader being a female sexual organ before it was replaced by the accurate code, which happened not too soon.

Bookmark on your Personal Space


Conversations About This Entry

There are no Conversations for this Entry

Entry

A85966366

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

References

h2g2 Entries

External Links

Not Panicking Ltd is not responsible for the content of external internet sites

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more