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:

>[-<+>]<
Open in the playground8 characters

What it needs

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
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Add, using up y3 + 5y2
Add, keeping y6 + 13y3
Subtract, using up y3 + 5y2
Subtract, keeping y6 + 13y3
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Add, using up yx = 3; y = 488232
Add, using up yx = 200; y = 100885032
Add, keeping yx = 3; y = 4819583
Subtract, using up yx = 9; y = 488232
Subtract, keeping yx = 9; y = 4819583

Other forms

Add, keeping y

x = x + y, y stays.

>[-<+>>+<]>[-<+>]<<
Open in the playground19 characters
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.

>[-<->]<
Open in the playground8 characters
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.

>[-<->>+<]>[-<+>]<<
Open in the playground19 characters
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

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.