home / blog / podcasts / videos / notes / photos / about / more

Programming in Brainfuck

Happy 2014!

Posted by Jeena

I just found an interesting question for this new year at StackExchange:

Produce the number 2014 without any numbers in your source code

The shortest answer wins and there are 5 bytes long answers already so I can not win this no matter what I do.

But I thought that this would be finally a task where the programming language brainfuck could shine! So I installed a Brainfuck interpreter and wrote this code which even gave the right answer the first time I run it!


Here is the documented code, what you need to know is that Brainfuck is actually really easy. The memory is like a very big array and you can go with ">" to the next item in the array and with "<" to the previous. With "+" you can increase the integer on the place where you currently are in the array and with "-" you can decrease it. With "." you can output the ASCII character equivalent of the integer where you currently are.

There is one thing which helps dealing with bigger numbers where you don't want to write 48 "+" signes in your source code you can use "[" to start the while loop and "]" to end it. Every time at the beginning of the loop it checks if the integer, to which you're pointing right now, is something else than 0. If so, it runs the code within the loop. If not, it jumps to the end of the loop wihtout executing it.

+++++ +++  # increment a[0] 8 times so a[0] == 8
[          # while (a[0]) {
 >         #    goto a[1]
  +++++ +  #    increment a[1] 6 times so a[1] = a[1] + 6
           #    that way a[1] ends up at 48 -> ASCII 0
           #    which is a good starting point for us
 <         #    goto a[0]
  -        #    decrement a[0] once that way we run the
           #    while loop 8 times before it is 0
]          # }
>          # goto a[1]
 ++        # increment a[1] two times so it is 50 -> ASCII 2
 .         # print out a[1]
 --        # decrement a[1] two times so it is 48 -> ASCII 0
 .         # print out a[1]
 +         # increment a[1] once so it is 49 -> ASCII 1
 .         # print out a[1]
 +++       # increment a[1] three times so it is 52 -> ASCII 4
 .         # print out a[1]

I'm posting it here because I can't post it on StackExchange because I need 10 points to post an answer to this question.

Have you written a response? Let me know the URL:

There's also indie comments (webmentions) support.