Code patternsFirst movesSet a constant
Set a constant
Puts a fixed number in a cell, in as few characters as possible.
Watch it run
The form “A product and a rest” works on 2 cells, named “n” and “scratch”. Press Play, drag the step slider forwards and back, or change n with the slider:
The figure needs JavaScript. The program below is the result for the example values.
One + adds one. For a large number that is a long text, so we let a loop do the adding: a rounds of b make a × b, and a few more + or − fix the rest.
The loop needs a counter, so it costs one scratch cell. The text is then about 2 × √n characters long instead of n.
For really large numbers (wide cells) the number is written in base 256: each byte is made small and shifted up by a loop that multiplies by 256.
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 target cell must be zero before, or the constant is added to what is there.
- The loop forms need one scratch cell that is zero, and leave it zero.
- The "wrap" form needs 8-bit wrapping cells. It gives a different number on any other size.
Common mistakes
- Using a short form from a table of constants without checking the cell size it was made for.
- Counting characters only. On an optimising interpreter every form here is one to three steps; in plain steps the loop form is slower than the long row of plus signs.
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 |
|---|---|---|
| A product and a rest | about 2 × √n characters; plain steps grow with n | 3 with a loop, 1 without |
| One plus per unit | n | 1 |
| By wrapping (8-bit cells only) | 465 | 156 |
| Base 256, for wide cells | grows with n | 1 for the lowest byte, 2 for each further byte |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| A product and a rest | n = 10 | 8 | 10 | 10 | 1 |
| A product and a rest | n = 65 | 8 | 24 | 108 | 3 |
| A product and a rest | n = 100 | 8 | 27 | 153 | 3 |
| A product and a rest | n = 255 | 8 | 39 | 333 | 3 |
| One plus per unit | n = 65 | 8 | 65 | 65 | 1 |
| By wrapping (8-bit cells only) | no values to choose | 8 | 15 | 465 | 156 |
| Base 256, for wide cells | n = 1,000 | 32 | 497 | 1,017 | 3 |
| Base 256, for wide cells | n = 50,000 | 32 | 537 | 50,977 | 3 |
Other forms
One plus per unit
The longest text, and the fewest plain steps.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Run this form step by step
The form “One plus per unit” works on one cell, named “n”. Press Play, drag the step slider forwards and back, or change n with the slider:
The figure needs JavaScript. The program below is the result for the example values.
By wrapping (8-bit cells only)
Makes 48, the code of the digit 0. It starts at 255, and 255 ÷ 5 is 51. The result is in cell 1.
-[>+<-----]>---
Run this form step by step
The form “By wrapping (8-bit cells only)” works on 2 cells, named “counter” and “48”. Press Play, drag the step slider forwards and back:
The figure needs JavaScript. The program below is the result for the example values.
Base 256, for wide cells
One short piece per byte of the number. Cells 1 and 2 are scratch cells.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[-<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]
Run this form step by step
The form “Base 256, for wide cells” works on 3 cells; the named ones are “n” and “scratch”. Press Play, drag the step slider forwards and back, or change n with the slider:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- Print a constant string is this pattern, once per letter.
- Brainfuck basics, lesson 5, "Building numbers", builds a number as a product. Build toward a chatbot, lesson 3, "Multiplying by a fixed number", does it for a big number, and Build toward a chatbot, lesson 8, "Size", shows why the length of the text matters for the chat program.
- In the project:
addConstin emit.js andaddBigin patterns.js.
Sources
- Source: Esolang wiki: Brainfuck constants: a table of the shortest known program for every number from 0 to 255; the wrap form here is checked against that idea, not copied.
- Source: This project: emit.js and patterns.js: the base-256 form is addBig.
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.