Questions tagged [brainfuck]

Brainfuck (bf) is an esoteric, Turing-complete programming language famous for its minimalistic, eight-character syntax.

Brainfuck is an esoteric and turing complete programming language famous for its minimalistic eight character syntax.

Brainfuck uses a simple machine model consisting of the program and instruction pointer, and an array of at least 30,000 byte cells initialized to zero. There is a movable data pointer (initialized to point to the leftmost byte of the array), and two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).

Language syntax

There are 8 language commands, each represented with a single character:

  • > - increment the data pointer (to point to the next cell to the right)
  • < - decrement the data pointer (to point to the next cell to the left)
  • + - increment (increase by one) the byte at the data pointer
  • - - decrement (decrease by one) the byte at the data pointer
  • . - output a character, the ASCII value of which being the byte at the data pointer
  • , - accept one byte of input, storing its value in the byte at the data pointer
  • [ - if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command
  • ] - if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.

All other characters in the source code are ignored, and there is therefore no special syntax for comments. Despite its minimalistic approach the language is Turing-complete, meaning that any algorithm can be implemented in Brainfuck.

Language semantics

Brainfuck language pseudocode

>   becomes     ++p;
<   becomes     --p;
+   becomes     ++*p;
-   becomes     --*p;
.   becomes     putchar(*p);
,   becomes     *p = getchar();
[   becomes     while (*p) {
]   becomes     }

Hello world Program

The following represents a program that outputs Hello, world! followed by newline \n:

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

The following represents same code with explanation of how it proceeds:

[ This program prints "Hello World!" and a newline to the screen, its
  length is 106 active command characters. [It is not the shortest.]

  This loop is a "comment loop", a simple way of adding a comment
  to a BF program such that you don't have to worry about any command
  characters. Any ".", ",", "+", "-", "<" and ">" characters are simply
  ignored, the "[" and "]" characters just have to be balanced. This
  loop and the commands it contains are ignored because the current cell
  defaults to a value of 0; the 0 value causes this loop to be skipped.
]
+++++ +++               Set Cell #0 to 8
[
    >++++               Add 4 to Cell #1; this will always set Cell #1 to 4
    [                   as the cell will be cleared by the loop
        >++             Add 2 to Cell #2
        >+++            Add 3 to Cell #3
        >+++            Add 3 to Cell #4
        >+              Add 1 to Cell #5
        <<<<-           Decrement the loop counter in Cell #1
    ]                   Loop till Cell #1 is zero; number of iterations is 4
    >+                  Add 1 to Cell #2
    >+                  Add 1 to Cell #3
    >-                  Subtract 1 from Cell #4
    >>+                 Add 1 to Cell #6
    [<]                 Move back to the first zero cell you find; this will
                        be Cell #1 which was cleared by the previous loop
    <-                  Decrement the loop Counter in Cell #0
]                       Loop till Cell #0 is zero; number of iterations is 8

The result of this is:
Cell No :   0   1   2   3   4   5   6
Contents:   0   0  72 104  88  32   8
Pointer :   ^

>>.                     Cell #2 has value 72 which is 'H'
>---.                   Subtract 3 from Cell #3 to get 101 which is 'e'
+++++++..+++.           Likewise for 'llo' from Cell #3
>>.                     Cell #5 is 32 for the space
<-.                     Subtract 1 from Cell #4 for 87 to give a 'W'
<.                      Cell #3 was set to 'o' from the end of 'Hello'
+++.------.--------.    Cell #3 for 'rl' and 'd'
>>+.                    Add 1 to Cell #5 gives us an exclamation point
>++.                    And finally a newline from Cell #6

This article is based on the one found in Wikipedia.

Read more

204 questions
9
votes
1 answer

Why does my program keep getting stuck while running the mandelbrot brainf*** program?

I wanted to improve my C skills, so I search some program's ideas. Someone propose to create a simple Brainf*** interpreter and then a compiler. So here I am. I created the interpreter and it works as expected, except with the Mandelbrot program: A…
tteixeira
  • 321
  • 2
  • 8
8
votes
3 answers

Brainfuck compiler in scala

Want to make some Domain Specific Language(DSL) for practice, first idea it is to write interpreter or compiler of Brainfuck. First idea was to override functions such as they will behave as Brainfuck commands: ">", "<", "+", "-", ".", ",", "[",…
Rinat Tainov
  • 1,479
  • 2
  • 19
  • 39
8
votes
1 answer

Source for simple programs in Brainfuck?

Is there a source where I can get multiple simple programs like addition, factorial, fibonacci and others for the brainfuck programming language? I know that there has been a question posted before here :…
Arjun J Rao
  • 925
  • 1
  • 10
  • 25
8
votes
2 answers

How to write if else statements in Brainfuck

I have just discovered a programming language, which is called Brainfuck. My question is how to write an if-else statement in Brainfuck? Is it done by comparing two cells? If yes, then how do I compare two cells in this program? Thank you
8
votes
3 answers

Implementing Control Structures in Brainfuck

For the uninitiated, Brainfuck is a Turing-complete language with only 8 commands, all of which have literal equivalents in C: bf c ---------------------- > ++ptr; < --ptr; + ++*ptr; - --*ptr; . putchar(*ptr); , …
Ryan Tenney
  • 1,812
  • 3
  • 16
  • 29
8
votes
2 answers

Print text multiple times in Brainfuck

I tried out this hello world program in Brainfuck. How can I print the text multiple number of times? Here's my code: +++++++[>++++++++++ <- ] >++.>++++++[>++++++++++ <- ] >+++++++++.>+++++++[>++++++++++ <- ] >++++++..>+++++++[>++++++++++ <- ]…
8
votes
3 answers

Making an if(x==y) statement in Brainfuck

So I'm working on a program that reads in a file and then outputs it back out again but i'm having trouble getting the program to stop taking input at the end of the file. I want it to stop at a specific character like '0' or '$' or anything really…
smithy545
  • 318
  • 3
  • 8
8
votes
7 answers

Smallest compiler possible in a turing complete language?

Brainfuck is known for its extremely small compilers. I have a VERY small device that probably couldn't fit even the smallest of brainfuck compilers in its data. Is there an esoteric programming language that has even smaller compilers than…
8
votes
1 answer

Brainfuck interpreter in Nimrod

I am toying with nim (at the time of writing still called nimrod), by writing a Brainfuck interpreter in the language. Without loops implemented, I have: import os, unsigned const RamSize = 200 type TRam = array[0..RamSize, int] var ram :…
8
votes
2 answers

Turing-completeness of a modified version of Brainfuck

Is Brainfuck Turing-complete if the cells are bits, and the + and - operations simply flip a bit? Is there a simple proof that Brainfuck-like languages are Turing-complete regardless of the cell size, or do I need to think of a program that…
Eric Yu
  • 161
  • 3
8
votes
1 answer

Options beyond RPython for writing interpreters w/ JITs?

I'm really interested in the PyPy project, but for the 1st (but less well-known) of its purposes listed below: A set of tools for implementing interpreters for interpreted languages An implementation of Python using this toolchain In the following…
lobsterism
  • 3,469
  • 2
  • 22
  • 36
7
votes
2 answers

Infinite Counting Loop in Brainfuck

As a debugger/test program for my brainf*ck implementation, I have created the following counting loop: +[[>+]+] With single byte, wrapping cells and with 30k cells with wraparound, this creates an infinite counting loop. It sets each cell to 1,…
bgrag
  • 73
  • 4
7
votes
1 answer

Efficient implementation of while loop in brainfuck

I am having trouble with implementing a brainfuck assembler for codegolf.se. I managed to load a string in to memory find its length cat it out, print strings n times etc, but I cant seem to load just the non lower case numbers into memory. So lets…
7
votes
2 answers

Of which things should I take care if I'm using unboxed type (like Int#) in Haskell / GHC?

I'm trying to write a small script which parses and executes Brainfuck code, to understand the GHC options of optimization, I'm trying to optimize the code in order to be a bit faster and to understand what's going on there. On of the parts is the…
fuz
  • 88,405
  • 25
  • 200
  • 352
7
votes
10 answers

Creating a Brainfuck parser, whats the best method of parsing loop operators?

I'm creating a Brainfuck parser (in a BASIC dialect) ultimately to create an interpreter but i've realise it's not as straight forward as i first thought. My problem is that i need a way to accurately parse the matching loop operators within a…
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
1
2
3
13 14