Becoming a better problem solver through BrainF**k part 1: What is BrainF**k?
vityavv (57)

See all 4 tutorials!


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:

+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
. 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
+++++++++ add nine
  < move back into the first cell
  ++++++++ add eight
  > 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
+++ add 3
[ 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.

You are viewing a single comment. View All

Finally, something actually productive and interesting on this site.

vityavv (57)

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