Code patternsInput and outputPrint a number in decimal

Print a number in decimal

Prints the value of a cell as digits, for example 173.

Watch it run

The form “A value from 0 to 255” works on 9 cells; the named ones are “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.

A cell holds a number; the screen wants letters. The digits are found by dividing by 10 (see divide, with remainder): the remainder is the last digit, the quotient is the rest.

So the last digit comes first. The digits are stored and then printed in the other order. Adding 48 turns a digit into its letter.

The 8-bit form knows there are at most three digits and writes the two divisions out. The form for any cell size loops: divide, store a digit, go on with the quotient until it is zero, then walk back printing (see find a zero cell or a marker).

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:

>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<
Open in the playground204 characters

What it needs

Common mistakes
  • Printing the cell itself with .: that prints the letter with that code, not the number.
  • Forgetting that the cost grows with the value, because division counts. Printing 1,000,000 on 32-bit cells takes millions of steps.

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 value from 0 to 255about 260 + 34 per unit of xabout 14 + 9 per unit of x
Any cell sizeabout 270 + 41 per unit of xabout 30 + 8 per unit of x
One digit823
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
A value from 0 to 255x = 0820426414
A value from 0 to 255x = 7820443970
A value from 0 to 255x = 17382046,1881,566
A value from 0 to 255x = 25582048,8442,296
Any cell sizex = 0816027231
Any cell sizex = 17381607,4031,440
Any cell sizex = 255816010,5542,095
One digitx = 7822823

Other forms

Any cell size

Loops until the quotient is zero. Layout: value, zero, then working cells.

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

The form “Any cell size” works on 10 cells; the named ones are “x” and “zero”. 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.

One digit

Adds 48 and prints. For a value from 0 to 9. The cell is left holding the letter.

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

The form “One digit” works on 2 cells, named “digit” and “scratch”. 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.