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:
[->+++<]
What it needs
- The factor is fixed when the program is written.
- The target gets the product added. Any cell size; too large a product wraps.
- To keep x: one scratch cell.
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| Using up x | 1 + (4 + f) × x | 1 |
| Keeping x | 6 + (14 + f) × x | 3 |
| In place | 4 + (4 + f) × x + 5 × the product | 3 |
| Several factors in one pass | 1 + 14x | 1 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Using up x | factor f = 3; x = 5 | 8 | 8 | 36 | 1 |
| Using up x | factor f = 12; x = 200 | 8 | 17 | 3,201 | 1 |
| Keeping x | factor f = 3; x = 5 | 8 | 23 | 91 | 3 |
| In place | factor f = 3; x = 5 | 8 | 16 | 114 | 3 |
| Several factors in one pass | x = 5 | 8 | 15 | 71 | 1 |
Other forms
Keeping x
Layout: x, y, scratch.
[->+++>+<<]>>[-<<+>>]<<
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.
[->+++<]>[-<+>]<
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.
[->++>+++>-<<<]
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
- Build toward a chatbot, lesson 3, "Multiplying by a fixed number", teaches it, with 90 × 100 as the example.
- This is how the chat program holds its weights: each weight is a factor in a loop like this. In the project it is
scatterandaddMulConstin emit.js. - Set a constant is this pattern with a fixed x.
Sources
- Source: This project: emit.js and patterns.js: scatter, addMulConst.
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.