Code patternsFirst movesCopy a value

Copy a value

Adds a cell to another cell and keeps the first one.

Watch it run

The form “Scratch cell on the right” works on 3 cells, named “x”, “copy” and “scratch”. 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.

Brainfuck can only read a cell by emptying it. So the first loop pours the value into two cells at once: the copy and a helper.

The second loop pours the helper back into the original. Now the value stands in two places and the helper is empty again.

A helper cell that is empty before and after is called a scratch cell.

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 playground21 characters

What it needs

Common mistakes
  • Using a scratch cell that is not empty. The original then comes back too large.
  • Forgetting the second loop. That is the form Into two cells at once of move a value, and the original is gone.

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
Scratch cell on the right6 + 15x3
Scratch cell on the left4 + 13x3
Two copies in one pass8 + 20x3
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Scratch cell on the rightx = 082163
Scratch cell on the rightx = 3821513
Scratch cell on the rightx = 2558213,8313
Scratch cell on the leftx = 3817433
Two copies in one passx = 3828683

Other forms

Scratch cell on the left

Layout: scratch, x, copy. The pointer starts on x.

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

The form “Scratch cell on the left” works on 3 cells, named “scratch”, “x” and “copy”. 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.

Two copies in one pass

One loop can feed as many cells as you like.

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

The form “Two copies in one pass” works on 4 cells; the named ones are “x”, “copy” and “scratch”. 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.