Code patternsArithmeticMultiply by a constant

Multiply by a constant

Adds a fixed multiple of a cell to another cell.

Watch it run

The form “Using up x” works on 2 cells, named “x” and “y”. Press Play, drag the step slider forwards and back, or change factor f and x with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

It is the pattern move a value, but it gives more than it takes: for every 1 that leaves x, the target gets 3.

The factor is written into the program as that many plus signs. A minus sign instead gives a negative factor.

The loop is flat, so an optimising interpreter does the whole multiplication in one step, for any x.

The code

This is the program the figure ran, written for these values. 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
  • Writing a large factor as hundreds of plus signs. Split it: two loops of 16 make 256.
  • Expecting x × y for two cells from this: the factor cannot come from the tape. See multiply two cells.

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
Using up x1 + (4 + f) × x1
Keeping x6 + (14 + f) × x3
In place4 + (4 + f) × x + 5 × the product3
Several factors in one pass1 + 14x1
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Using up xfactor f = 3; x = 588361
Using up xfactor f = 12; x = 2008173,2011
Keeping xfactor f = 3; x = 5823913
In placefactor f = 3; x = 58161143
Several factors in one passx = 5815711

Other forms

Keeping x

Layout: x, y, scratch.

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

The form “Keeping x” works on 3 cells, named “x”, “y” and “scratch”. Press Play, drag the step slider forwards and back, or change factor f and x with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

In place

x becomes f × x. Layout: x, scratch.

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

The form “In place” works on 2 cells, named “x” and “scratch”. Press Play, drag the step slider forwards and back, or change factor f and x with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

Several factors in one pass

Three targets get 2x, 3x and −x. One loop, one step.

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

The form “Several factors in one pass” works on 4 cells, named “x”, “2x”, “3x” 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

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.