Brainfuck Tutorial 09-15-2013, 08:36 AM
#1
Brainfuck
Brainfuck is simple, limited and inefficient coding language, which useless to create anything useful. Although you can amaze other people or entertain your brain with it: code is almost never clear and small things need big amount of code, which makes no sense with a quick glance.
Where you can find interpreter?
There are pretty marginally information about Brainfuck in the Internet and there are not too many interpreters for Brainfuck. Here's a link for working interpreter http://esoteric.sange.fi/brainfuck/impl/interp/i.html. I've made one interpreter with C, but I recommend using the link above. Interpreter is easy to make by yourself if you know other language: everything you need to know about Brainfuck is in this tutorial.
Basics
Brainfuck program usually uses about 30000 memory slots which usually have the size of one byte; in some cases the size may be different. In every memory slot, there is one integer, always zero at the beginning of the program. The basic operation of this language is to change the memory slot (characters < and >), changing the value by one (characters + and -), printing the slot's value as ASCII-character (.) and reading to memory slot (,) and a loop ( [ ] ) which will be repeated as longs the memory slot's value, which was chosen at the beginning, is non-equivalent to zero. Language is based on these 8 characters and every other character will be ignored. This way you don't have to write anything special to write a comment. Just make sure that your comment won't mess up the code.
Changing the memory slot: characters < and >
Greater than- and less than-characters are good to understand as an arrow to left and right. They will change the memory slot by one in the certain way. If we get out of the 30000 byte's memory table, the program will either crash or jump to the opposite edge of the table depending on the implementation. At first, the leftmost memory slot is chosen. You can choose whether you call it first or zeroth memory slot, but in this is tutorial we call it the zeroth.
Following code simply goes first three memory slots to right and then three back to its starting point:
Code:
>>> <<<Changing the value: characters + and -
Plus and minus-signs increase and decrease the value of the chosen memory slot by one. Since the slot is the size of one byte, it holds the value between [-128, 127] or [0, 255], depends on how you want to read it(if you want read more about this:LINK). When the value range is exceeded, the program will jump back to the first slot. When the program starts, all the slots are set to zero.
This code sets the values 0,1,2,3 to first four memory slots of the program(of course you don't have set the 0 value):
Code:
> + > ++ > +++Data transfer: characters . and ,
Period prints the memory slot's value and comma reads input from user. Slots are the size of one byte so one slot is one character. The value will be printed as a same sign, which depends on the used characters. A character's numerical value is 65, number 0 is 48.
This why we ask 4 characters from user and print them in reverse order:
Code:
,>,>,>,
.<.<.<.Loop: characters [ and ]
Square brackets defines the loop. From starting square bracket, program will check if memory slot is zero. If it is, loop will be skipped. If not, loop will be run. When program reaches the ending square bracket, it will jump back to the starting bracket. Often it's appropriate to return at the same memory slot where it left, when loop has ended; loop won't do it by itself.
This code prints the numbers from 0 to 9. At first we set the zero's value to be 10: loop must be executed 10 times. Then we set the first place's value to be zero's ASCII-code which is 48. Loop will always decrease the zeroth place, print the value of the first place and increase it by one. After ten times there are again zero at the zeroth place so the loop will end.
Code:
++++++++++
> ++++++++++++++++ ++++++++++++++++ ++++++++++++++++ <
[->.+<]Summing numbers
Summing is laborious. Since you can't move the memory slot's value at once, it must be move to other place one by one. Summing will happen by moving two memory slot's values to the same slot. In this small code we sum up 3 and 4. At first we put 3 to the zeroth slot and 4 to the first slot. In the loop, we decrease from the zeroth place and increase to second and fourth slot until zeroth place's value is zero. This way original value is still in the memory but it's stored in two other slots. Next we move the value of the first slot to second and third slot which makes the value of the second slot to be 7, third slot's value to be 4 and fourth slot's value to 3. And finally we add zero's ASCII-value to every slot which makes them to be printable.
Hope this picture makes it even bit more clear:
Code:
+++>
++++<
[->>+>>+<<<<]
>
[->+>+<<]
++++++++++++++++ ++++++++++++++++ ++++++++++++++++
[->+>+>+<<<]
>>>
.<.<.Complicated example
Here's simple program which counts Fibonacci's numbers and prints them in octal. Program has C++-style comments. Since Brainfuck handles all the characters it knows, all the + and - signs couldn't be used in comments.
Code:
// Print 1 and 1
// One in the zeroth slot and space in slot m13
// "1 1" will be printed
++++++++++++++++ ++++++++++++++++ ++++++++++++++++ + .
>>>>>>>>>>>>> ++++++++++++++++ ++++++++++++++++ .
<<<<<<<<<<<<< .
[-] // zero in the zeroth place
+++++ //How many times two numbers are counted?
>+ >+ << // m1 = 1 and m2 = 1
[-> // while (m0) choose m1
// Numbers movements; & means plus-sign
// m1 m2 m3
// a b
// a&b b
// a&b a & 2b
// a&b a & 2b
// a&b a & 2b
>[-<+>>+<] // m1 &= m2 ja m3 &= m2 ja m2 = 0
<[->+>>+<<<] // m2 &= m1 ja m4 &= m1 ja m1 = 0
>>>[-<<<+>>>] // m1 &= m4 ja m4 = 0
<[-<+>]> // m2 &= m3 ja m3 = 0
// Convert the number into octal format
>[-]>[-]>[-]>[-] // m5 m6 m7 m8 = 0
>[-]>[-]>[-]>[-] // m9 m10 m11 m12 = 0
<<<<<<<<<<<[->>>>+>+<<<<<] // m5 = m6 = m1 ja m1 = 0
>>>>>[-<<<<<+>>>>>] // m1 = m6 ja m6 = 0
<<<<[->>>>>>>+>+<<<<<<<<] // m9 = m10 = m2 ja m2 = 0
>>>>>>>>[-<<<<<<<<+>>>>>>>>] // m2 = m10 ja m10 = 0
<<<<< // choose m5
// Convert m5 into octals and place them into slots m6, m7 and m8
// (m6 = m5) and (m7 = 4 * m5) and (m8 = 32 * m5) and (m5 = 0)
// which means (m8 / 32 = m5 mod 8) and (m7 / 4 = m5 mod 64)
[->+>++++>++++++++++++++++++++++++++++++++<<<]
// (m5 = m7 / 4) and (m6 = m6 minus m7 / 4) and (m7 = 0)
>>[----<<+>->]
// (m7 = m8 / 32) and (m8 = 0)
>[--------------------------------<+>]
// (m8 = m7) ja (m5 = m5 minus m7) and (m7 = 0)
<[->+<<<->>]
// (m7 = m6 / 8) and (m6 = 0)
<[-------->+<]
// (m6 = m5 / 8) and (m5 = 0)
<[-------->+<]
// (m5 = m7 / 8) and (m7 = 0)
>>[--------<<+>>]
// Into printable format
++++++++++++++++++++++++++++++++++++++++++++++++
[-<<+>+>>+<]
// Print the space from m13 and then octal number
>>>>>>.<<<<<<
<<.>.>>.
// Same action for the numbers in slots m9 m10 m11 m12
>[->+>++++>++++++++++++++++++++++++++++++++<<<]
>>[----<<+>->]
>[--------------------------------<+>]
<[->+<<<->>]
<[-------->+<]
<[-------->+<]
>>[--------<<+>>]
++++++++++++++++++++++++++++++++++++++++++++++++
[-<<+>+>>+<]
>>.<<
<<.>.>>.
// Back to the slot m0 and to start of the loop
<<<<<<<<<<<
<]Same program without line breaks or comments
Code:
++++++++++++++++ ++++++++++++++++ ++++++++++++++++ +.>>>>>>>>>>>>>+ ++++++++++++++++ +++++++++++++++. <<<<<<<<<<<<<.[- ]+++++>+>+<<[->> [-<+>>+<]<[->+>> +<<<]>>>[-<<<+>> >]<[-<+>]>>[-]>[ -]>[-]>[-]>[-]>[ -]>[-]>[-]<<<<<< <<<<<[->>>>+>+<< <<<]>>>>>[-<<<<< +>>>>>]<<<<[->>> >>>>+>+<<<<<<<<] >>>>>>>>[-<<<<<< <<+>>>>>>>>]<<<< <[->+>++++>+++++ ++++++++++++++++ +++++++++++<<<]> >[----<<+>->]>[- ---------------- ---------------< +>]<[->+<<<->>]< [-------->+<]<[- ------->+<]>>[-- ------<<+>>]++++ ++++++++++++++++ ++++++++++++++++ ++++++++++++[-<< +>+>>+<]>>>>>>.< <<<<<<<.>.>>.>[- >+>++++>++++++++ ++++++++++++++++ ++++++++<<<]>>[- ---<<+>->]>[---- ---------------- ------------<+>] <[->+<<<->>]<[-- ------>+<]<[---- ---->+<]>>[----- ---<<+>>]+++++++ ++++++++++++++++ ++++++++++++++++ +++++++++[-<<+>+ >>+<]>>.<<<<.>.> >.<<<<<<<<<<<<]Epilogue
That was Brainfuck. Simple and limited language, that it is hard to use for real programs. It is very good brain bender since you have to think even with the simplest things. If you're interested, please visit here: http://esolangs.org/wiki/Brainfuck


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)