4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Hello world in brianfuck

Brainfuck is a Turing Complete language that only has everything a programmer needs; C looks bloated compared to brainfuck. Here's some example code:

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

Beautiful, I know. But, this doesn't teach you how it works. That's what this post is going to change.

+ : Increments the value at the current cell by one.
- : Decrements the value at the current cell by one.
> : Moves the data pointer to the next cell (cell on the right).
< : Moves the data pointer to the previous cell (cell on the left).
. : Prints the ASCII value at the current cell (i.e. 65 = 'A').
, : Reads a single input character into the current cell.
[ : If the value at the current cell is zero, skips to the corresponding ] .
    Otherwise, move to the next instruction.
] : If the value at the current cell is zero, move to the next instruction.
    Otherwise, move backwards in the instructions to the corresponding [ .

Above taken from https://learnxinyminutes.com/docs/brainfuck/

Ok, looks easy right? Here's how you write hello world:
+++++++ [ > ++++++++++ < - ] > ++ .

Actually this just prints H because writing hello world would take me 20 hours to type.

First, we increment cell 1 to 7.
Then we move to cell 2, enter the loop, and increment it 10 times.
We go back to cell 1 and decrement it once.
This happens 7 times.
When the loop is done, cell 1 is at 0, cell 2 is at 60. The loop is done, so we increment cell #2 by 2, making it 72.
the . then prints the current cell, which is 72, which is also the ascii value for H

Easy right?

1 upvotes $0.00

Replies (3)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: