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:
| |
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
| |
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:
| |
The Comma and Space
Getting from ‘o’ (111) to ‘,’ (44) requires subtracting 67:
| |
Then from ‘,’ (44) to space (32), subtract 12:
| |
Continuing to “World!”
From space (32) to ‘W’ (87), we add 55:
| |
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:
| |
Expected Output
Hello, World!
Alternative: Compact Version
The code above is formatted for readability. Here’s a one-line version:
| |
ASCII Reference
Here are the ASCII values for “Hello, World!”:
| Character | ASCII Value |
|---|---|
| H | 72 |
| e | 101 |
| l | 108 |
| o | 111 |
| , | 44 |
| (space) | 32 |
| W | 87 |
| r | 114 |
| d | 100 |
| ! | 33 |
Common Patterns
Adding a Value
To add a specific value to a cell, use multiple + characters:
| |
Subtracting a Value
| |
Clearing a Cell
| |
Copying a Value
| |
Debugging Tips
- Count your brackets - Every
[needs a matching] - Track your pointer position - It’s easy to lose track of which cell you’re on
- Start simple - Begin with shorter strings before attempting full programs
- Use comments - Any character except the 8 commands is ignored
A Simpler First Program
If “Hello, World!” feels overwhelming, try printing just “H”:
| |
That’s 72 + characters to set a cell to 72 (ASCII for ‘H’), then . to print it.
Or more efficiently using a multiplication loop:
| |
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