Learn to Code via Tutorials on Repl.it

← Back to all posts
Becoming a better problem solver through BrainF**k part 1: What is BrainF**k?

See all 4 tutorials!

Introduction

BrainF**k is called an "esoteric langeuge" because it is technically a programming language but it isn't widely used due to the way it is designed. BrainF**k is one of the earliest and most famous esoteric languages namely because of its simplicity.

BrainF**k works along a "strip" of memory---basically, there are an infinite number of "cells" in a row that you can move forward and backward through. BrainF**k interpreters only recognize 8 symbols, as follows:

symbolmeaning
`+`add one to the current cell's value
`-`subtract one from the current cell's value
`>`move to the right by one cell
`<`move to the left by one cell
`,`take the input (one character of it) and put it in the current cell
`.`print out the ASCII character associated with the current cell's value
`[`If the current cell is zero, skip to the matching `]`
`]`If the current cell is not zero, go back to the matching `[`

Those last two may be the most confusing, but they are essentially how BrainF**k makes decisions, which makes it Turing Complete and allows it to become a programming language. They can be simplified by thinking of `[...]` as a loop, as we will see with our first BrainF**k program, but, as we will learn later on, that will not always be the case.

Hello, World!

Let's write our first program. Actually, instead of making it say "Hello, World!" let's just make it say "Hi" for now. To make BrainF**k output a letter, you have to know the ASCII number associated with that letter. You can use a table like the one below to do so. In it, we can see that "H" is 72 and "i" is 105. First, here is an example program without loops:

``````++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ add 72 into the current cell (BrainF**k ignores anything but the seven symbols)
. print out H
+++++++++++++++++++++++++++++++++ add another 33 to get 105
. print out i``````

Obviously, though, this approach is tedious. So, let's look at how we can improve our method, using loops! Before we start, know that 72 is 9 8, and 33 is 3 11

``````> move into the second cell
[
< move back into the first cell
> move to the second cell again
- subtract one
] with these square brackets, eight will be added to the first cell nine times until the second cell reaches zero(comma) at which point BrainF**K will continue executing code
< move back into the first cell
. print "H"
> move to the second cell
[ start a loop like the one before
<+++++++++++>- same procedure as the loop above(comma) but with 13 instead of 8
]
. print "i"``````

If you need additional help wrapping your head around it, I suggest this excellent tool, which allows you to see the execution happen in real time. However, this tutorial has come to a close. In the next tutorial, I will discuss how BrainF**K helps you become a better programmer, and dissect a BrainF**k program to better understand it.

sanjaykdragon (171)

Finally, something actually productive and interesting on this site.

vityavv (46)

@sanjaykdragon There are 2 more tutorials and a 3rd on the way!

oof I saw this in the eloquent javascript series (the egg). Nice info though (wouldn't use BrainFk for anything). Is it possible to write a game using BrainFk? It would be cool if we could make one

vityavv (46)

@AdCharity People have made games with BrainF**k. I think I saw a guy make tic-tac-toe once. Since the language is Turing Complete, you can do anything with it that you can do with another language (provided there's text input and output). Actually, there once was someone who made an IRC bot (a chat bot) in BrainF**k, connecting it to IRC with another program.