Code patternsFirst movesMove a value

Move a value

Adds a cell to another cell and leaves the first one empty.

Watch it run

The form “To the next cell” works on 2 cells, named “x” and “y”. Press Play, drag the step slider forwards and back, or change x and y with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

Each round takes 1 from the source and gives 1 to the target. The loop stops when the source is zero, so by then everything has arrived.

The target gets the value added. If it held 2 before, it holds 2 more than the source did.

A loop like this one only adds and moves and comes back to where it started. That is called a flat loop. An optimising interpreter does it in one step.

The code

This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:

[->+<]
Open in the playground6 characters

What it needs

Common mistakes
  • Unequal numbers of > and <. The loop then tests a different cell every round and wanders off.
  • Expecting the source to survive. Use copy a value for that.

What it costs

A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:

The figure needs JavaScript. The table below has the measured steps.

Cost as a formula, and measured
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
To the next cell1 + 5x1
Two cells away1 + 7x1
Into two cells at once1 + 8x1
To the left1 + 5x1
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
To the next cellx = 0; y = 08611
To the next cellx = 4; y = 086211
To the next cellx = 200; y = 3861,0011
Two cells awayx = 488291
Into two cells at oncex = 489331
To the leftx = 486211

Other forms

Two cells away

The same, with a longer walk. Every round pays for the distance twice.

[->>+<<]
Open in the playground8 characters
Run this form step by step

The form “Two cells away” works on 3 cells, named “x”, “other” and “target”. Press Play, drag the step slider forwards and back, or change x with the slider:

The figure needs JavaScript. The program below is the result for the example values.

Into two cells at once

Both targets get the value. This is the first half of copy a value.

[->+>+<<]
Open in the playground9 characters
Run this form step by step

The form “Into two cells at once” works on 3 cells, named “x”, “first” and “second”. Press Play, drag the step slider forwards and back, or change x with the slider:

The figure needs JavaScript. The program below is the result for the example values.

To the left

The pointer starts on the right cell.

[-<+>]
Open in the playground6 characters
Run this form step by step

The form “To the left” works on 2 cells, named “target” and “x”. Press Play, drag the step slider forwards and back, or change x with the slider:

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.