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:
[->+>+<<]>>[-<<+>>]<<
What it needs
- One scratch cell that is zero. It is zero again at the end.
- The copy is added to the target: clear the target first if it may hold something.
- Any cell size. The pointer ends where it started.
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| Scratch cell on the right | 6 + 15x | 3 |
| Scratch cell on the left | 4 + 13x | 3 |
| Two copies in one pass | 8 + 20x | 3 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Scratch cell on the right | x = 0 | 8 | 21 | 6 | 3 |
| Scratch cell on the right | x = 3 | 8 | 21 | 51 | 3 |
| Scratch cell on the right | x = 255 | 8 | 21 | 3,831 | 3 |
| Scratch cell on the left | x = 3 | 8 | 17 | 43 | 3 |
| Two copies in one pass | x = 3 | 8 | 28 | 68 | 3 |
Other forms
Scratch cell on the left
Layout: scratch, x, copy. The pointer starts on x.
[->+<<+>]<[->+<]>
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.
[->+>+>+<<<]>>>[-<<<+>>>]<<<
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
- Brainfuck basics, lesson 7, "Moving and copying", teaches it in its section "Keep the original". Build toward a chatbot, lesson 2, "Moving and copying", teaches it again and has you change the program yourself.
- Every "keeping the source" variation in this library is a copy first.
- In the project:
copyin emit.js.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "x = y".
- Source: This project: emit.js and patterns.js: copy 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.