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:

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

What it needs

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
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
A product and a restabout 2 × √n characters; plain steps grow with n3 with a loop, 1 without
One plus per unitn1
By wrapping (8-bit cells only)465156
Base 256, for wide cellsgrows with n1 for the lowest byte, 2 for each further byte
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
A product and a restn = 10810101
A product and a restn = 658241083
A product and a restn = 1008271533
A product and a restn = 2558393333
One plus per unitn = 65865651
By wrapping (8-bit cells only)no values to choose815465156
Base 256, for wide cellsn = 1,000324971,0173
Base 256, for wide cellsn = 50,0003253750,9773

Other forms

One plus per unit

The longest text, and the fewest plain steps.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Open in the playground65 characters
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.

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

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

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.