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
148
votes
6 answers

How does the Brainfuck Hello World actually work?

Someone sent this to me and claimed it is a hello world in Brainfuck (and I hope so...) ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. I know the basics that it works by moving a…
speeder
  • 6,197
  • 5
  • 34
  • 51
34
votes
7 answers

Optimisation for a brainfuck interpreter

As an exercise to help me learn about interpreters and optimisation, neither of which I know anything about, I have written a brainfuck interpreter in C. It appears to work flawlessly thus far, though it does not compete well in execution speed…
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
32
votes
3 answers

Compiler C to Brainfuck (for harassing a professor)?

A professor of mine has said he'll accept homework assignments in any language we'd care to use. I'm on good enough terms that I'd like to mess with him a bit and submit a valid homework assignment using brainfuck, whitespace, or some equally…
Sevenless
  • 2,805
  • 4
  • 28
  • 47
25
votes
9 answers

Detecting infinite loop in brainfuck program

I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is, the program turns out to have an infinite loop in a sizeable number of…
Sundar R
  • 13,776
  • 6
  • 49
  • 76
22
votes
6 answers

Brainfuck compare 2 numbers as greater than or less than

How can I compare two numbers with an inequality? (greater than or less than) I want to compare single digits For example 1 2 5 3 9 2 etc.
Krzysztof Lewko
  • 982
  • 7
  • 24
20
votes
2 answers

C to brainfuck compiler?

I have a basic program in C that I wish to convert to the language brainfsck but cannot find anything on the subject. I find many "brainfuck to C" converters but not the other way around. I found C2BF but do not understand how to use it or if that's…
MyNameIsKhan
  • 2,594
  • 8
  • 36
  • 56
19
votes
3 answers

Port dos2unix to brainfuck

I got into an argument over on SuperUser.com about useless answers and found myself challenging the other poster to answer the question in brainfuck. He didn't take me up on it, but now I'm curious. All the program needs to do is convert CRLF line…
quack quixote
  • 573
  • 1
  • 4
  • 14
19
votes
2 answers

Printing a number in brainfuck?

I've searched for a while, but i couldn't find anything that could help me. Let's say the first cell(or value, etc.) equals 165. How do i print "165"? My idea was to cut the number into seperate pieces: 1,6 and 5. It would than be no problem to…
Lars
  • 500
  • 1
  • 7
  • 22
16
votes
2 answers

How to read multi digit numbers in brainfuck

I want to read in a number with any number of digits with bf. I know how to read in the correct number of digits if I set it manually, like this: ,>,>, 2 Read in 3 digits << 0 -------- -------- -------- -------- -------- -------- 45 decrements >…
annonymously
  • 4,708
  • 6
  • 33
  • 47
15
votes
1 answer

Handling loops in Brainf*ck

I tried testing my Brainfuck interpreter in c with this bottle shaped code in Brainfuck: +>+++++++[>>>+++ +++++<<<<+++++ +++>-]+++++++++>>> +>>++++++++++[>+ …
lol
  • 231
  • 1
  • 10
14
votes
4 answers

Making a JIT compiler

I've written a Brainfuck implementation (C++) that works like this: Read input brainfuck file Do trivial optimizations Convert brainfuck to machine code for the VM Execute this machine code in the VM This is pretty fast, but the bottleneck is now…
orlp
  • 112,504
  • 36
  • 218
  • 315
14
votes
4 answers

what does the '~' mean in python?

what does the '~' mean in python? i found this BF interpreter in python a while ago. import sys #c,i,r,p=0,0,[0]*255,raw_input() c=0 i=0 p=raw_input() r=[0]*255 while c":i+=1 if m=="<":i-=1 …
hidroto
  • 1,313
  • 3
  • 10
  • 9
13
votes
6 answers

How would one go about testing an interpreter or a compiler?

I've been experimenting with creating an interpreter for Brainfuck, and while quite simple to make and get up and running, part of me wants to be able to run tests against it. I can't seem to fathom how many tests one might have to write to test…
Steven Raybell
  • 503
  • 2
  • 5
  • 14
11
votes
5 answers

Implementing Brainfuck loops in an interpreter

I want to build a Brainfuck (Damn that name) interpreter in my freshly created programming language to prove it's turing-completeness. Now, everything is clear so far (<>+-,.) - except one thing: The loops ([]). I assume that you know the (extremely…
sub
  • 2,653
  • 3
  • 27
  • 29
11
votes
7 answers

How to calculate the sum of 2 numbers with BrainFuck

I'm trying to write a program with BrainFuck that can read two numbers up to 9, calculate the sum of them and then print the result out, e.g. 3 & 5 give the result 8 . I'm just trying to understand the BF language but it looks much harder than I…
Yahoo
  • 119
  • 1
  • 1
  • 3
1
2 3
13 14