Brainfuck Typo

I’ve always been fascinated by the Brainfuck programming language, probably because out of all the weird languages I’ve heard of, it’s the one that makes the most sense to me. I’ve read about it a few times in the past, and was pretty sure I knew how it worked, until reading the Mateas and Montfort (hereafter M&M) piece and running across this snippet of code, which they say is a “Hello, World” program:

++++++++++[>+++++++>++++++++++>+++>+<<<<>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

What’s the problem? Here:

++++++++++  [  >+++++++>++++++++++>+++>+<<<<>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

That doesn’t make any sense (well, it makes less sense than a Brainfuck program should) – it has an open bracket without a close bracket! I actually think M&M do a pretty good job of describing the language in general: it creates a large array of byte cells, “>” goes right, “<” goes left, “+” increments, “-” decrements, “.” outputs, “,” inputs, “[” jumps forward to “]” if the byte is 0, “]” jumps back to “[” if it’s non-zero. However, they don’t bother to go into more detail (which I don’t really blame them) about what exactly that all does in practice. The output command uses ASCII, which just converts bytes into characters using this table:

So really, all that Brainfuck does is start moving pointers around that table. A simple program to print out “helloworld” could just keep adding until it gets to “h” (104) and then add/subtract from there in order to spell out the words – this is kind of like spelling something out on a screen using a remote where all you can do is go left or right (curse you Apple TV!!!). The only problem is that you’ll get a program that looks like this:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---.+++++++..+++.++++++++.--------.+++.------.--------.

It’s a lot easier to start off the program with a [ loop to get some of that pesky addition taken care of more compactly. Think about it as a while loop: the byte before the [ (our counter byte) sets how many times we want it to run, and then we can move to the next byte (our target byte), increment some amount, go back, decrement our counter, and repeat, just like in Python. So if we want to add 100, it’s a lot more succinct to add 10 and loop it 10 times. This makes our “helloworld” program look like this:

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

Which is a lot nicer. This is what the M&M “Hello World!” program is setting itself up to do, it just forgets to decrement its counter and close the loop, like this:

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

This program actually works now (you can check it using this online Brainfuck interpreter)! It uses 5 bytes: byte 0 for counting, byte 1 for upper case, byte 2 for lower case, byte 3 for the space and exclamation point, and byte 4 for the new line (which is pretty standard for most of these programs). It starts off by setting the counter to 10, and then loops through to set byte 1 to 70 (right in the ballpark of “H” at 72), byte 2 to 100 (close to “e” at 101), byte 3 to 30 (near space at 32), and byte 4 to 10 (which happens to be new line exactly) – this is why the plus signs inside the brackets are in groups of 7, 10, 3, and 1:

++++++++++[             byte 0 = 10
>+++++++                byte 1 = 7         (*10 = 70)
>++++++++++             byte 2 = 10        (*10 = 100)
>+++                    byte 3 = 3         (*10 = 30)
>+                      byte 4 = 1         (*10 = 10)
<<<<-]                  decrement byte 0

From that point on, it’s just a matter of spelling things out using bytes 1-4:

>++.                    H                  byte 1
>+.+++++++..+++.        ello               byte 2
>++.                    (space)            byte 3
<<+++++++++++++++.      W                  byte 1
>.+++.------.--------.  orld               byte 2
>+.                     !                  byte 3
>.                      (new line)         byte 4

So now the programmer in me can breathe a sigh of relief knowing that the code works. I really love the taxonomy of weird code that M&M put together, as well as the examples they give for each: insane (INTERCAL), minimalist (Brainfuck), playful (Shakespeare), and impossible (Malbolge). The one other type of language I would have loved to see them talk about more would be funges, which use two-dimensional (or more!) space to direct their pointers. These can be things that still look like languages, just laid out differently, like Befunge:

 >25*"!dlrow ,olleH":v
                  v:,_@
                  >  ^

Or they can not look like languages at all. My favorite is probably Piet, name for Dutch painter Piet Mondrian, that actually uses colors in a bitmap to make things that really look more like paintings than programs. Here’s a “Hello World” program in Piet:

So cool! There’s a great gallery of sample programs “written” in Piet here if you all want to see more.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website