Code patternsFirst movesSet a cell to zero
Set a cell to zero
Empties the cell under the pointer, whatever it held.
Watch it run
The form “Count down” works on one cell, named “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.
The loop takes 1 from the cell and asks "is it zero?". It goes round until the answer is yes.
So the plain cost grows with the value: two steps for every unit. An optimising interpreter sees a loop that only counts its own cell down and sets the cell to zero in one step.
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:
[-]
What it needs
- Any cell size. No scratch cells. The pointer does not move.
- On cells that do not wrap, the value must not be negative, or
[-]never ends.
Common mistakes
- Forgetting it before a "set to a constant".
+++adds 3, it does not set 3. - Clearing a cell that is known to be zero already. It is harmless, but it is one more step every time.
- Using
[+]on wide cells: it counts up to the top, which takes billions of plain steps on 32-bit 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 |
|---|---|---|
| Count down | 1 + 2x | 1 |
| Count up | 1 + 2 × (cell size − x), or 1 when x = 0 | the same: this loop is not folded |
| Three cells in a row | 7 + 2 × (a + b + c) | 4 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Count down | x = 0 | 8 | 3 | 1 | 1 |
| Count down | x = 5 | 8 | 3 | 11 | 1 |
| Count down | x = 255 | 8 | 3 | 511 | 1 |
| Count up | x = 5 | 8 | 3 | 503 | 503 |
| Count up | x = 250 | 8 | 3 | 13 | 13 |
| Three cells in a row | a = 4; b = 0; c = 9 | 8 | 13 | 33 | 4 |
Other forms
Count up
Counts up until the cell wraps round to zero. Needs wrapping cells.
[+]
Run this form step by step
The form “Count up” works on one cell, named “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.
Three cells in a row
Clears a block and walks back.
[-]>[-]>[-]<<
Run this form step by step
The form “Three cells in a row” works on 3 cells, named “a”, “b” and “c”. Press Play, drag the step slider forwards and back, or change a, b and c with the sliders:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- Inside almost every other pattern, for example if non-zero.
- Brainfuck basics, lesson 4, "Loops", has you empty a cell this way, in its section "Empty a cell".
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "x = 0".
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.