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
2
votes
1 answer

Printing out 178-character string in 270 characters of brainf*ck

I’m trying to print out a 178-character string in brainfuck. This wouldn’t be a problem except I’m limited to using 270 characters of brainfuck. I was thinking of hashing the 178-character string using a two-way hashing function, but I've been…
A A
  • 29
  • 2
2
votes
2 answers

Sum of number in brainfuck

I would like to know if it is possible to calculate the sum of 1+2+3+...+k in brainfuck just with the number k at the beginning of the code? For example is it possible to do 1+2+3 like this: +++> (here the code creates a two add it with the three,…
ParaH2
  • 519
  • 1
  • 4
  • 21
2
votes
1 answer

Implementing Brainf*ck loops with JavaScript

So i is the instruction pointer and ptr is the data pointer. I'm trying to get this right: [ - 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…
kvhadzhiev
  • 123
  • 2
  • 8
2
votes
2 answers

BF - Generate pseudo-random number

I'm looking for the code to assign a pseudo-random number to a memory cell in the esoteric language brainf***. I found this sample code, but found it somewhat confusing. From what I could tell it was a "some assembly required" (No pun intended?)…
rootmeanclaire
  • 808
  • 3
  • 13
  • 37
2
votes
1 answer

Program is ignoring input

I'm trying to write a simple brainfuck interpreter in C++. It works great so far, but it ignores the character input command (','). The Interpreter: #include #include #include using namespace std; #define SIZE…
DelishusCake
  • 107
  • 1
  • 5
1
vote
1 answer

Smallest compiler ever

Yesterday, I stuck in the internet with this article about programming language called BrainFuck. http://www.muppetlabs.com/~breadbox/bf/ So what is wonder me is this Brainfuck is the ungodly creation of Urban Müller, whose goal was apparently to…
1
vote
1 answer

Copy byte value from an address to the specified address

I have this bf code: > ++++ ++ @1=6 How do I copy current value (6) in address @1 to the specified address for example address @8 I can do it with this: > ++++ ++ @1=6 < @0 [ >>>> >>>> @8+=1 <<<< <<<< @1-=1 ] In this line, @1=0 and @8=6 It…
1
vote
1 answer

Why does this interpreter results in a segmentation fault?

I am trying to write a brainf*ck Interpreter in C. The current pointer, the array and the length of the array are given in a struct. The interpreter method receives pointers to the array struct and the brainf*ck instruction as a string. If the…
1
vote
2 answers

Prints '+' to the screen 20 times using a loop and arithmetic operators

I have a simple Brainfuck program called "PuraPuraNanya", which prints the character + to the screen 20 times using a loop and arithmetic operators. ++++++++++[>+>+++>++++>+++++<<<<-]>++. Here are explanations of what the program does: ++++++++++…
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
1
vote
1 answer

Is there a way to print out the value at the pointer an a number in brainfuck?

So, i was wondering how could i print this value as a number NOT as a character an example would be that the pointer is on (125) (0)(0)(125)(0)(0) In this case when this snippet of would start, the value at the pointer will be printed out as…
1
vote
1 answer

How to optimize brainf*ck instructions

I'm trying to write an optimisation feature for my brainf*ck interpreter. It basically combines same instructions into 1 instruction. I wrote this function but It doesn't work properly: pub fn optimize_multiple(instructions: &Vec) ->…
d3r1n
  • 45
  • 1
  • 6
1
vote
2 answers

Brainfuck interpreter using cellular automata

Does anyone have a set of cellular automata rules for a brainfuck interpreter? I assume it would it be similar to implementations of a universal turing machine. Those exist on wolfram site but I don't know how to tweak them for a BF system.
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203
1
vote
1 answer

implementing loops in a c brainfuck interpreter

I have written a simple brainfuck interpreter in c. all the commands work fine, except for loops. currently, I am handeling them like this: I have a function to find the matching square bracket: int findbracket(char *commands, int currentpos){ …
1
vote
1 answer

Exit condition loop BrainF*ck

I've used the BrainFuck Visualizer to check out this small piece of code but I don't understand how the exit conditions work on the loops starting with -. It suddenly just stops and jumps to the next statement. Could anyone explain this to…
Mout Pessemier
  • 1,665
  • 3
  • 19
  • 33
1
vote
1 answer

getchar() taking the last char from previous printf()?

I'm writing a compiler/interpreter for the esoteric language brainf*ck (I'm not too sure on StackOverflow's profanity policy, so I'll censor myself until somebody tells me I don't have to), and I'm running into a very mysterious (to me, at least)…