Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Brainfuck

(thing) by Schemer (1.8 y) (print)   ?   (I like it!) 2 C!s Sat Jun 17 2000 at 10:03:40

Here's the whole language:
  • '>' : move the memory pointer to the next cell,
  • '<' : move the memory pointer to the previous cell,
  • '+' : increment the memory cell under the memory pointer,
  • '-' : decrement the memory cell under the memory pointer,
  • ',' : fills the memory cell under the memory pointer with the ASCII value of next character from the input,
  • '.' : writes the contents of the memory cell under the memory pointer as a character with the corresponding ASCII value,
  • '[' : moves to the command following the matching ']', if the memory cell under the memory pointer is zero, and
  • ']' : moves to the command following the matching '[', if the memory cell under the memory pointer is not zero.

(thing) by ccunning (3.4 mon) (print)   ?   (I like it!) 1 C! Tue Sep 05 2000 at 3:00:41

I have no idea if this formatting will work, but it's worth a shot... Here is a 99 bottles of beer on the wall program in brainfuck. Also worth noting, the homepage of brainfuck is currently at: http://www.catseye.mb.ca/esoteric/bf/

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



(idea) by Senso (18.5 hr) (print)   ?   (I like it!) Wed Jun 20 2001 at 22:50:38

A small tribute to Douglas Adams that I wrote in Brainfuck. I can't believe I spent one hour writing this. But anyway, compile it and it'll show a famous sentence from the master. Yeah that's right, all that crap for a single sentence. All hail Brainfuck. Whatever.


>+++++++[<++++++++++>-]<.>+++++[<
+++++>-]<++.>+++++[<+++>-]<++.
[-]>++++++++[<++++>-]<.>++++++++[
<++++++++++>-]<-.++++++.-.[-]>++++++++[
<++++>-]<.>+++++++[<++++++++++>-]<
+++.+++++.[-]>++++++++[<++++>-]<.>
++++++++[<++++++++++>-]<++++.>----[<+++>
-]<.---.[-]>++++++++[<++++>-]<.>
++++++++++[<++++++++>-]<+++++.-------.>-----[<
++>-]<-.+++++.-------.>+++++[<++++>-]<
---.++.>-----[<+++>-]<.-.[-]>++++++++[<
++++>-]<.>++++++++[<++++++++>-]<
++.-.++.++++++++.>++++[<+++>-]<.>----------[<
++>-]<--.>+++[<++++++>-]<+.>-----[<
+++>-]<.+++++++++++++.+.[-]>++++++++[<++++>
-]<.>++++t+++++++[<+++++++>-]<++.---------.[
-]>++++++++[<++++>-]<.>++++++++[<++++++++++>
-]<++++.>----[<+++>-]<.---.[-]>
++++++++[<++++>-]<.>++++++++++[<++++++++>
-]<+++++.-------.--------.-----.>++[<+++++++++>-]<
.>-----[<++>-]<-.+.++++++.-.>----[<+++>
-]<-.+.++++++++++.-------.[-]>++++++++[<++++>-]<
.>++++++++[<++++++++>-]<+++++.+++++++++.>---[<
+++>-]<-.[-]>++++++++[<++++>-]<.
>++++++++++[<+++++++>-]<+++++++++.---------.[-]>
++++++++[<++++>-]<.>++++++++[<++++++++++>-]<
++++.>----[<+++>-]<.---.[-]>++++++++[<
++++>-]<.>++++++++[<+++++++++++>-]<-.>--[<
+++++++++>-]<.>+++++++[<++>-]<.+.>-----[<
+++>-]<.>++++[<+++>-]<+.----.[-]>
++++++++[<++++>-]<.>++++++++++[<++++++++>-]<
+++.---.-------.+++++++++.>----[<++++>-]<-.>++++[<
+++>-]<-.[-]>++++++++[<++++>-]<.>
++++++++[<++++++++>-]<+.>++++[<++++>-]<
+.-----.[-]>++++++++[<++++>-]<.>+++i++++++[<
++++++++>-]<+++++++.---------.[-]>++++++++[<++++>
-]<.>++++++++[<++++++++++>-]<++++.>----[<+++>
-]<.---.[-]>++++++++[<++++>-]<.>++++[<
++++++++++>-]<-.>+++++[<+++++>-]<+.+++++++++++.
-----------.>+++++[<++++>-]<+++.+.[-]>++++++++[<
++++>-]<.>[-]++++++++[<++++++++++>-]<
----.---.----.>++++[<+++>-]<++.[-]>++++++++[<
++++>-]<.>+++++++++++++[<+++++>-]<.[-]>
++++++++[<++++>-]<.>+++++++++[<+++++++++>-]<
++.------.------------.+++++++++++..[-]>++++++++[<++++>-]<
.>++++++++[<++++++++++>-]<+++++.-------.++++.>----[<
+++>-]<-.++.------.>++++[<++++>-]<+.>-------[<
++>-]<.+.-.[-]>++++++++[<++++>-]<.>
+++++++++++[<++++++++>-]<+.>-----[<++++>-]<
.+++++++..+++.++++++++.[-]>++++++++[<++++>-]<.>
++++++++++[<++++++++!>-]<+++.++.-------.[-]>+++++++++[<
+++++>-]<+.[-]>++++++++[<++++>-]<.[-]>
+++++++++[<+++++>-]<.[-]>++++++++[<++++++++>-]<
++++.>++++++++[<+++++>-]<+++.++++++.>------[<++>-]<
--.+++++.>-----[<++>-]<-.>++[<+++++++++>-]<
.[-]>++++++++[<++++>-]<.[-]>+++++++++++++[<
+++++>-]<.>+++++[<+++++++>-]<.---.>++++[<+++>
-]<.++++++.[-]++++++++++.>++++++++++[<++++++++>-]<+++.


(thing) by silencio (2.9 y) (print)   ?   (I like it!) 2 C!s Tue Jul 10 2001 at 13:52:13

Brainfuck


Brainfuck was invented in 1993 by the swiss programmer Urban Müller. He wanted to create a Turing-complete programming language with a small compiler. His original compiler has 240 bytes of C code, the version I wrote has 2.400 bytes of Pascal code ... (okay, I am ashamed ...).

As already noded, BF has eight different operators with which you are forced to create your programs. It works indeed, but as soon as you try to write non-trivial programs, you'll either turn completely mad or your brain will be fucked, which is quite the same ;-)


Here is my version of the Hello World program in Brainfuck:

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

(thing) by Jetifi (1.4 wk) (print)   ?   (I like it!) 2 C!s Wed May 15 2002 at 13:08:44

Brainfuck is quite a brilliant little programming language. As noted above and elsewhere, it operates in a similar manner to a basic Turing machine. A note on the name: understandably, Urban Müller now wishes he'd called it something different.

Below is the code for a Brainfuck interpreter written in Java. The usage is:

	java Brainfuck yourCode.b

Where yourCode.b is a file containing Brainfuck. Standard input and standard output are used for the ',' and '.'. To pipe a file into your program (or anyone else's), you can do this at a shell:

	cat yourFile | java Brainfuck yourCode.b 1

...I think.

The code could be better, but it could also be worse. It was written to be understandable, rather than terse - for that, see the writeup below. There you can see an interpreter done in 3 lines of obfuscated C. (Which is cool, but consider which one is easier to debug or maintain :-P) This would probably make for a good tournament of Perl Golf.

That said:

import java.io.InputStream;
import java.io.PrintStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Stack;

class Brainfuck {

    InputStream input;
    PrintStream output;

    ArrayList infiniteTape;
    int headPosition;

    Brainfuck( InputStream in, PrintStream out ) {
        input = in;
        output = out;

        infiniteTape = new ArrayList();

        for( int i = 0; i != 50; i++ ) infiniteTape.add( new Byte( (byte)0x00 ) );

        headPosition = 0;
    }

    void interpret( byte[] code ) throws IOException {

        int instructionPointer = 0;

        Stack nestedLoops = new Stack();

        while( instructionPointer != code.length ) switch( code[instructionPointer] ) {

            case '>' :
                headPosition++;
                if( headPosition >= infiniteTape.size() ) stretchTapeEnd();
                instructionPointer++;
                break;
            case '<' :
                headPosition--;
                if( headPosition <= 0 ) stretchTapeStart();
                instructionPointer++;
                break;
            case '+' :
                setSymbol( (byte)(getSymbol() + 0x01) );
                instructionPointer++;
                break;
            case '-' :
                setSymbol( (byte)(getSymbol() - 0x01) );
                instructionPointer++;
                break;
            case ',' :
                setSymbol( (byte)input.read() );
                instructionPointer++;
                break;
            case '.' :
                output.print( (char)getSymbol() );
                instructionPointer++;
                break;
            case '[' :
                if( getSymbol() != 0x00 ) {
                    nestedLoops.push( new Integer( ++instructionPointer ) );
                } else instructionPointer = skipLoop( code, instructionPointer );
                break;
            case ']' :
                if( nestedLoops.size() == 0 )
                  throw new IOException ("mismatched ]");

                if( getSymbol() != (byte)0x00 ) {
                    instructionPointer = ((Integer)nestedLoops.peek()).intValue();
                } else {
                    nestedLoops.pop();
                    instructionPointer++;
                }
                break;
            default : instructionPointer++; break;
        }
    }

    byte getSymbol() {
        Byte symbol = (Byte)infiniteTape.get( headPosition );
        return symbol.byteValue();
    }

    void setSymbol( byte value ) {
        Byte symbol = new Byte( value );
        infiniteTape.set( headPosition, symbol );
    }

    void stretchTapeEnd() {
        for( int i = 0; i != 20; i++ )
          infiniteTape.add( new Byte( (byte)0x00 ) );
    }

    void stretchTapeStart() {
        for( int i = 0; i != 20; i++ )
          infiniteTape.add( 0, new Byte( (byte)0x00 ) );
        headPosition += 20;
    }

    int skipLoop( byte[] code, int from ) {
        int subLoops = 0;

        do switch( code[++from] ) {
            case '[' : subLoops++; break;
            case ']' : subLoops--; break;
            default  : break;
        } while( subLoops != -1 );

        return ++from;
    }

    public static void main( String[] args ) throws IOException {

        byte[] code;

        if( args.length < 1 ) {
            System.out.println( "Usage: java Brainfuck file.b" );
            return;
        }

        File brainfuckFile = new File( args[0] );

        if( !brainfuckFile.exists() ) {
            System.out.println( args[0] + " does not exist" );
            return;
        }

        code = new byte[(int)brainfuckFile.length()];

        FileInputStream fileIn = new FileInputStream( brainfuckFile );

        for( int i = 0; i != code.length; i++ ) code[i] = (byte)fileIn.read();

        fileIn.close();

        Brainfuck interpreter = new Brainfuck( System.in, System.out );
        interpreter.interpret( code );

    }

}

And there you go. Now, you too can marvel at ''hello, world!'', and wonder just how on earth ccunning checked for the difference between singular and plural when counting bottles of beer on a wall. This is just the first step, of course; the logical progression would be to write a Brainfuck interpreter entirely in Brainfuck...

...but someone's already beaten me to it:

http://home.wxs.nl/~faase009/Ha_bf_inter.html

1: Thanks to ivarneli for the correction.


(thing) by Chilliwilli (5.6 y) (print)   ?   (I like it!) 1 C! Thu Aug 01 2002 at 0:24:01

Three line C brainfuck interpretter
From http://www.catseye.mb.ca/esoteric/bf/

char m[9999],*n[99],*r=m,*p=m+5000,**s=n,d,c;main(){for(read(0,r,4000);c=*r;
r++)c-']'||(d>1||(r=*p?*s:(--s,r)),!d||d--),c-'['||d++||(*++s=r),d||(*p+=c==
'+',*p-=c=='-',p+=c=='>',p-=c=='<',c-'.'||write(2,p,1),c-','||read(2,p,1));}

Compile with: gcc abovecode.c -o bf
Use with: bf < source.bf


printable version
chaos

A practical introduction to brainfuck A cool short C program Compressed Brainfuck Perl Golf
Hello World 99 bottles of beer on the wall mindfuck Befunge
INTERCAL lameness filter &#46; Turing Machine
E2 killed my dog, and I don't think it's fair ASCII porn Obfuscated C Books that will induce a mindfuck
Guns don't kill people. Wait, guns do kill people. Elephant Memory Systems Turing tar-pit malbolge
Operation Mindfuck programming language Ook! quine
No more writeups are being accepted for this node. If you've something to add here that hasn't already been mentioned, put it in your scratch pad and message an administrator for a review. If you feel you have something to add to this node, post it on your Scratch Pad and contact an editor.
  Epicenter
Login
Password

password reminder
register

Everything2 Help


cooled by doyle

Cool Staff Picks
Just another sprinkling of indeterminacy
Uh, are you looking at my joystick to impress her, or are you just an asshole?
Dead Can Dance
t.A.T.u.
Homosexual adoption
Reflections of yourself
Oxford Movement
The world breaks everyone
public key cryptography
Saxony
It's Time for Me to Die
Film Editing
Six Sigma
reading Japanese
New Writeups
antigravpussy
One fly amongst many(person)
sam512
Moon Base Shackleton, 1978(fiction)
Pavlovna
toy boy(person)
XWiz
tear jerker(review)
Heitah
Anarchy is Order(idea)
jessicaj
July 26, 2008(dream)
Berek
ABBA(person)
devolution
k-hole(place)
Nadine_2
The Sound Of Madness(review)
SwimmingMonkey
Conversations with Fo Fo, the Loneliest dog in Purgatory(fiction)
locke baron
lynx(thing)
Simulacron3
Reality, Dimensions and the Natural Ontology(essay)
SubSane
Making Love to a 9-Foot Woman(person)
Ouzo
Thoughts(idea)
antigravpussy
I fall silent, listening. The breadcrumbs are talking about us(person)
This affordable entertainment brought to you by The Everything Development Company