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
4
votes
4 answers

Finding out if a number in a cell is even or odd

Given that a number in the 0th cell of tape is filled and the rest are all just used as scratch cells (i.e. they all start at 0 and are temporaries -- I don't care what happens to them), I would like to replace the 0th cell with a 0 or a 1. 0 if…
Jay Bosamiya
  • 3,011
  • 2
  • 14
  • 33
4
votes
1 answer

Brainfuck. How check on palindrome?

The problem is to check if sequence is palindrome using Brainfuck. input is a sequance of numbers output 0 if it is not palindorme, else 1. I have one idea: Say, we have sequance 1 2 3 2 1. We can memorise first cell from our array in variable(do…
d40a
  • 154
  • 9
4
votes
1 answer

Divmod algorithm in brainfuck

Can somebody please explain this code to me? I understand what it does but I don't understand how it works. # >n 0 d [->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<] # >0 n d-n%d n%d n/d
Dr. John A Zoidberg
  • 1,168
  • 2
  • 14
  • 25
4
votes
3 answers

GOTO instruction in Brainf***

There is an extended version of bf which has a goto instruction, ?. I know that, in theory, it should be possible to simulate a goto in the classic 8 instruction version of bf. How can I do that in practice? Is there an existing bf goto pattern or…
vz0
  • 32,345
  • 7
  • 44
  • 77
4
votes
2 answers

Cannot figure out if statement

Here is the code i am working with, it tests for the input of 'b' and should output 'Y' if there was anything else inputted. Please, nobody ask why I chose to use 'Y' for a false value.. > , < +++++ +++++ [ > ----- ---- < - ] > ----- ---…
phyrrus9
  • 1,441
  • 11
  • 26
4
votes
1 answer

Better way to implement interpreter in Python

I am attempting to implement an interpreter for brainfuck and as of now, I am just using a series of if/elif statements. if(i == ">"): ... elif(i == "<"): ... elif(i == "+"): ... elif(i == "-"): ... However, this seems very clunky…
cjm
  • 814
  • 1
  • 11
  • 26
4
votes
2 answers

state chart of brainfuck interpreter

i have written an alpha version of an brainfuck ide. i wrote my own interpreter although i had massive help from my teacher regarding loops in the code because i had a really hard time understanding it in the "IT way". now for my report i need a…
LeonidasFett
  • 3,052
  • 4
  • 46
  • 76
4
votes
2 answers

What's wrong with my brainfuck parser code?

I'm trying to make a program in Java that can read, compile, and run brainfuck source files (.bf). I've gotten it to work just fine with Wikipedia's Hello World example, but it breaks on the ROT13 example (claims it reached an unmatched ] when it…
Ky -
  • 30,724
  • 51
  • 192
  • 308
3
votes
2 answers

HTTP response was too large: 10485810. The limit is: 10485760

i have written an online brainfuck interpreter ..!! the problem is when i take the text input , it gives an error !!... HTTP response was too large: 10485810. The limit is: 10485760. it seems the max limit of gae is 1mb.. how can i get around it !1…
Hick
  • 35,524
  • 46
  • 151
  • 243
3
votes
2 answers

Is brainfuck still Turing complete if opening brackets do nothing

I am working on a physical breadboard 8bit CPU that directly interpret brainfuck. The language specification indicates that both opening and closing brackets have logic : [ => Jump to matching ] If Zero ] => Jump to matching [ Unless Zero But…
Gatoyu
  • 642
  • 1
  • 6
  • 22
3
votes
2 answers

How do I make a program in Brainfuck that will add two digits?

I'm trying to make a program in Brainfuck which I think is also called "Brainflake", that will add two single digit decimal integers input with ASCII numeric characters and display the sum in ASCII numeric characters in the output. How would I go…
Jose
  • 31
  • 2
3
votes
2 answers

How do I implement the looping functionality in my BrainFuck Interpreter?

There's multiple questions here already, but I'll still proceed. This is a simple BrainFuck interpreter. I figured out all the other symbols, but I can't figure out how to implement loops. Can anyone help? package com.lang.bfinterpreter; import…
justanotherguy
  • 399
  • 5
  • 16
3
votes
1 answer

Can anyone explain why this brainfuck if/else code doesn't work?

// if x = y then {if_code}; else then {else_code} ,>,< / empty memory [->-<]> / (x)'y [>->]< / 0'(y-x) +[ {if_code} ->> ]< / {if x=y then} (0)'0; {else then} 0'y-x'(1) [ {else_code} [-] ]< / 0'(y-x) / empty memory / is to show what…
user17301834
  • 443
  • 1
  • 8
3
votes
0 answers

Are BrainF*ck tape spots overflowing?

Can I assume that if you do - on a tape spot with 0, that it loops around to 255? Otherwise I don't understand how you can start a loop with - without first incrementing? e.g: +[----->+++<]>+.+.[--->+<]>---.+[----->+<]>.++.--.
Mout Pessemier
  • 1,665
  • 3
  • 19
  • 33
3
votes
2 answers

Optimized cell increasing in brainfuck

So my aim: put the value n into a cell with smallest amount of instructions. I could do + twenty times for the value 20. But a shorter way is for example to do >++++[<+++++>-]<. How could I calculate the optimized value setter (assuming that the…
dev_null
  • 71
  • 6