Code patternsFirst movesAdd and subtract two cells
Add and subtract two cells
Adds one cell to another, or takes it off.
Watch it run
The form “Add, using up y” 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.
Adding is the pattern move a value: every unit that leaves y arrives in x. Subtracting is the same loop with a − at the target.
To keep y, the loop also feeds a scratch cell and a second loop moves that back, as in copy a value.
Nothing here looks at the size of the numbers. If the sum does not fit, the cell wraps round; if the difference is negative, it wraps the other way. That is the right answer in the arithmetic of the 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
- Any cell size. The "keep" forms need one scratch cell that is zero.
- The pointer ends where it started.
- A negative result is only meaningful on wrapping cells, read as a signed number.
Common mistakes
- Expecting subtraction to stop at zero. It does not: 3 − 5 is 254 on 8-bit cells. For a floor at zero use the saturating subtract.
- Looping on x and adding to y when you wanted the sum in x.
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 |
|---|---|---|
| Add, using up y | 3 + 5y | 2 |
| Add, keeping y | 6 + 13y | 3 |
| Subtract, using up y | 3 + 5y | 2 |
| Subtract, keeping y | 6 + 13y | 3 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Add, using up y | x = 3; y = 4 | 8 | 8 | 23 | 2 |
| Add, using up y | x = 200; y = 100 | 8 | 8 | 503 | 2 |
| Add, keeping y | x = 3; y = 4 | 8 | 19 | 58 | 3 |
| Subtract, using up y | x = 9; y = 4 | 8 | 8 | 23 | 2 |
| Subtract, keeping y | x = 9; y = 4 | 8 | 19 | 58 | 3 |
Other forms
Add, keeping y
x = x + y, y stays.
>[-<+>>+<]>[-<+>]<<
Run this form step by step
The form “Add, keeping y” works on 3 cells, named “x”, “y” and “scratch”. 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.
Subtract, using up y
x = x − y, y = 0.
>[-<->]<
Run this form step by step
The form “Subtract, using up y” 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.
Subtract, keeping y
x = x − y, y stays.
>[-<->>+<]>[-<+>]<<
Run this form step by step
The form “Subtract, keeping y” works on 3 cells, named “x”, “y” and “scratch”. 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.
Where it is used
- Brainfuck basics, lesson 7, "Moving and copying", adds two cells in its section "Adding". Build toward a chatbot, lesson 3, "Multiplying by a fixed number", adds up products this way.
- Every sum in the chat program is a row of these loops with a factor: see multiply by a constant.
Sources
- Source: Esolang wiki: Brainfuck algorithms: sections "x = x + y" and "x = x − y".
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.