Advanced

Hello World in Brainfuck

Your first Brainfuck program - the classic Hello World example with Docker setup

Writing “Hello, World!” in Brainfuck is a rite of passage for anyone interested in esoteric programming languages. What takes one line in most languages requires careful planning and execution in Brainfuck.

The Code

Create a file named hello.bf:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.
>+.
+++++++..
+++.
-------------------------------------------------------------------.
------------.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++++++++.
+++.
------.
--------.
-------------------------------------------------------------------.

Understanding the Code

Let’s break down how this works. Brainfuck operates on an array of memory cells. We need to output each character by setting a cell to its ASCII value and printing it.

The Setup Loop

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

This initialization loop runs 10 times and sets up our memory cells:

  • Cell 1: 70 (close to ‘H’ = 72)
  • Cell 2: 100 (close to ’e’ = 101, ’l’ = 108, ‘o’ = 111)
  • Cell 3: 30 (for spacing calculations)
  • Cell 4: 10 (for newlines if needed)

Printing Each Character

After setup, we fine-tune each cell value and print:

1
2
3
4
>++.        Move to cell 1, add 2 (70+2=72='H'), print
>+.         Move to cell 2, add 1 (100+1=101='e'), print
+++++++..   Add 7 (101+7=108='l'), print twice
+++.        Add 3 (108+3=111='o'), print

The Comma and Space

Getting from ‘o’ (111) to ‘,’ (44) requires subtracting 67:

1
-------------------------------------------------------------------.

Then from ‘,’ (44) to space (32), subtract 12:

1
------------.

Continuing to “World!”

From space (32) to ‘W’ (87), we add 55:

1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++.

And so on for the remaining characters, adjusting cell values up or down as needed.

Running with Docker

The easiest way to run Brainfuck without installing an interpreter locally:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Pull the Brainfuck interpreter image
docker pull sergiomtzlosa/brainfuck:latest

# Create the source file
cat > hello.bf << 'EOF'
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.
EOF

# Run the program
docker run --rm -v $(pwd):/code sergiomtzlosa/brainfuck /root/brainfuck/brainfuck /code/hello.bf

Expected Output

Hello, World!

Alternative: Compact Version

The code above is formatted for readability. Here’s a one-line version:

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

ASCII Reference

Here are the ASCII values for “Hello, World!”:

CharacterASCII Value
H72
e101
l108
o111
,44
(space)32
W87
r114
d100
!33

Common Patterns

Adding a Value

To add a specific value to a cell, use multiple + characters:

1
+++++     Add 5 to current cell

Subtracting a Value

1
-----     Subtract 5 from current cell

Clearing a Cell

1
[-]       Set current cell to 0 (loop: decrement until zero)

Copying a Value

1
[->+>+<<]>>[-<<+>>]<<    Copy cell 0 to cell 1 (using cell 2 as temp)

Debugging Tips

  1. Count your brackets - Every [ needs a matching ]
  2. Track your pointer position - It’s easy to lose track of which cell you’re on
  3. Start simple - Begin with shorter strings before attempting full programs
  4. Use comments - Any character except the 8 commands is ignored

A Simpler First Program

If “Hello, World!” feels overwhelming, try printing just “H”:

1
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.

That’s 72 + characters to set a cell to 72 (ASCII for ‘H’), then . to print it.

Or more efficiently using a multiplication loop:

1
>++++++++[<+++++++++>-]<.    8 * 9 = 72 = 'H'

Historical Note

Urban Muller’s original Brainfuck compiler was written in 1993 for the Amiga in just 240 bytes of machine code. This extreme minimalism was the whole point - proving that a useful (if impractical) programming language could have an incredibly tiny implementation.

The fact that we can run this same language 30+ years later in a Docker container on any modern operating system is a testament to how simple and portable the design truly is.

Next Steps

Try modifying the Hello World program to print your name, or explore other Brainfuck programs to see more complex techniques.

Running Today

All examples can be run using Docker:

docker pull sergiomtzlosa/brainfuck:latest
Last updated: