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:
[->+<]
What it needs
- Any cell size. No scratch cell.
- The loop must end on the cell it started on: as many
>as<. - Clear the target first if you want "set", not "add".
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| To the next cell | 1 + 5x | 1 |
| Two cells away | 1 + 7x | 1 |
| Into two cells at once | 1 + 8x | 1 |
| To the left | 1 + 5x | 1 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| To the next cell | x = 0; y = 0 | 8 | 6 | 1 | 1 |
| To the next cell | x = 4; y = 0 | 8 | 6 | 21 | 1 |
| To the next cell | x = 200; y = 3 | 8 | 6 | 1,001 | 1 |
| Two cells away | x = 4 | 8 | 8 | 29 | 1 |
| Into two cells at once | x = 4 | 8 | 9 | 33 | 1 |
| To the left | x = 4 | 8 | 6 | 21 | 1 |
Other forms
Two cells away
The same, with a longer walk. Every round pays for the distance twice.
[->>+<<]
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.
[->+>+<<]
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.
[-<+>]
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
- Brainfuck basics, lesson 7, "Moving and copying", teaches it first. Build toward a chatbot, lesson 2, "Moving and copying", builds it step by step, starting from an empty tape.
- In the project it is
movein emit.js, a special case ofscatter. Almost everything in the chat program is made of scatters.
Sources
- Source: This project: emit.js and patterns.js: move and scatter in emit.js.
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.