Programming in Brainfuck

Building Blocks

Here we take a look at some building blocks for Brainfuck programs. I'm not going into painful detail about these. They're all pretty easy to understand (after all, it's an easy language) and there's no better way to get a feel for a language then to play with it.


Move a value to one (or more) locations

Examples:

Move a value from a[p] to a[p+1].

[>+<-]
Move a value from a[p] to a[p+2] and a[p+4].
[>>+>>+<<<<-]


Copy a value to one (or more) locations

A copy is a multi-move followed by a move back to the original value's location.

Examples:

Simple copy from a[p] to a[p+1] (using a[p+2] as a work area).

[>+>+<<-]>>[<<+>>-]
Copy from a[p] to a[p+1] and a[p+2] (using [p+3] as a work area).
[>+>+>+<<<-]>>>[<<<+>>>-]


Multiply a value by a fixed amount

A multiply is a move with multiple increment at the target.

Example:

Multiply three (a[p]) times five.

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


Multiply a value by another value

This requires multiple copies and nested loops.

Example:

Multiply three (a[p]) times five (a[p+1]) and place the result in a[p+2].

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


Traditionally, since Brainfuck dealt only with byte values using unsigned arithmetic, there were no issues vis-a-vis negative numbers. Many implimentations (my own amongst them), however, allow for values which exceed one byte, supporting signed arithmetic. Should you wish to use any of these 'expanded' Brainfuck implementations, all the above can be adapted for use with negative numbers, etc...

Aw, your smart... You'll figure it out!

"OK. I'm ready play around with this. Do I have to roll my own interpreter or do you have something I can use?"

Geez... Nobody ever wants to do any extra work :).

Have a look here for some Brainfuck resources.


Home | Introduction | First Program | Input Processing | Building Blocks | Resources | bf2pl

cydathria dot com