##### BrainF Advanced Tutorial

BrainF*** is a very fun and intuitive language. Through only 8 simple characters, you can do almost anything a normal programming language can do. This tutorial is going to show you more advanced things in BrainF and will hopefully get you thinking about all of the things possible in BrainF.

# What is BrainF

BrainF was made by Urban Müller in 1993, and was designed to make programming as hard as possible. It only uses these 8 symbols: `+ - < > [ ] , .`

This is an advanced tutorial though, so if you don't know how to use BrainF at all, check out @minx28 's tutorial that he made 2 years ago: https://repl.it/talk/learn/BrainF-A-Complete-Guide/8885

In this tutorial, you will see a table setup as `{(number) number}`. What this means is that the pointer is on whatever the parentheses is around, and the second number is just a set number. If there is a 0 in one of the spaces, that means that you cannot set that to something else such as 1 or 2. For instance `{5 (3)}` means that the pointer is on a value with 3, and the previous value is a 5.

Also, some BrainF algorithms will require more than just 1 or 2 cells (values). For instance, multiplication needs 4 cells to be arranged such as `{2 (4) 0 0}` and only then will it return `{(8)}`. When the input needs 4 cells but it only outputs 1 or 2, this means that the other cells that you inputted were set to 0.

# Simple Stuff

There are a few simple algorithms that you can easily identify and need to know. They have a multitude of uses and are important in most advanced algorithms.

• Setting Pointer to 0: `[-]`
• Move to First 0 Before or At Pointer: `[<]`
• Move to First 0 After or At Pointer: `[>]`
• Set Next Cell to Current Value: `>[-]<[->+<]`
• Move Cell to Next 2 Cells: `[->+>+<<]`
• Copy Cell to Next Cell: `[->+>+<<]>>[-<<+>>]<<`
• Reset All Values Between 0's: `[>][[-]<]`

Important: Always keep your first pointer at 0, so that you can always move back to it if needed

# Basic Operators

In BrainF, there are no operators, you can only increment or decrement a value. This makes it insanely hard to do simple maths such as division or exponents. To do those things, you need to build off of other things.

The 2 easiest operators you can make are addition and subtraction. These will take 2 cells, the starting value, and the operating value, and it will be setup as: `{ 4 (2) }`.

``[-<+>]<``
• Subtraction:
``[-<->]<``

These both will output `{(answer) 0}` so for addition you would get `{(6) 0}` and subtraction would get `{(2) 0}`. The way this works is by lowering the operating value, and for each decrement, it will operate on the previous value, so for addition the table would be:

``````{(4) 2}
{(5) 1}
{(6) 0}``````

Now that you have these 2 basic operators, this gives so much more power. Lets try multiplication.

Multiplication is simply repeated addition, so if we loop through the operating value and each iteration make a copy of the starting value and add it to a result starting out at 0, we can get the result. This takes 4 cells: {2 (3) 0 0}

• Multiplication:
``<[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[<<+>>-]<<``

This will start out by decrementing the operating value so that we can loop through it and stop it at 0, and then it will go forward and add itself to the next 2 values `[->+>+<<]`. It then will go forward twice and move the copied value back to its original position `>>[-<+>]` and move the pointer back to the operating value.

All of the stuff after that `>[-]>[<<+>>-]<<` is just to move around the values to return an end value of `{(answer)}`.

Division and modulo can be done in the same algorithm, and it takes a lot of cells. You will need 6 cells in an arrangement {11 (4) 0 0 0 0}.

• Div/Mod
``````<[->[->+>>]>[<<+>>[-<+>]>+>>]<<<<<]
>[>>>]>[[-<+>]>+>>]<<[-<<<+>>>]<<<``````

This is a very complicated algorithm, but what it does is subtract a value until either the operating value or the main value is set to 0, and will return an arrangement of {(2) 1 3}
The reason the second value is 1 is because it is 4-3. It returns the division floored, the operating value minus the modulo, and then the modulo.

# Usage

These 4 basic operators creates an entire new area of innovation in BrainF. Pretty much all possible maths operations such as the trigonometric functions, square roots, and exponents, can be created by using these 4 operations. For instance, a factorial can be made with only using 5 cells.

``````Input: {(5) 0 0 0 0}

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

Output: {(120) 5}``````

# Limitations

In BrainF, there are so many limitations that are set. For one, it is BrainF. There are only 8 symbols and something that can be done in 2 lines in C will take at least 20 lines. There are also no variables. There is only one "infinitely" long line of cells (usually caps around 100k), and you can only move left or right once at a time. The value inside each cell can only be an integer as well. (In normal BrainF*** it caps at 255, but in Repl.it BrainF which was written in Javascript, it can go on past that, and also be negative. Don't worry about passing the integer limit though because that is near impossible. You can only increment and decrement; it would take around 36 minutes to even pass the 32 bit mark. 53 bits is a whole other story. (P.S. 143 years))

This means that there are no floats, which make up usually 75% of all variable types. If you wanted a challenge you could try implementing floating point representation.

Also, BrainF uses ASCII representation, so if you tried to print out the value 69 to the console, it would print `E` instead of 69. This adds a whole layer of complexity to BrainF, as printing 69 would require the ASCII of 6, then the ASCII of 9, which can hardly be derived from 69 in BrainF. Logging values over 10 could be another challenge.

Thanks for checking out this tutorial, I hope this helped you on your journey in BrainF-ing, as this programming language is one of my favorites, up there with Node.JS

Also, I am looking to make a language built on top of BrainF called BrainFrick, and it would be like a step up from BrainF and would be transpiled to BrainF so if anyone would like to help, I'm up for a challenge.

Anyways, hope you learned something, and good luck -Spicy

You are viewing a single comment. View All