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:
>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<
What it needs
- 8-bit form: eight cells; the value is kept; made for cells that hold 0 to 255.
- Any-size form: the value, a zero cell, then about three cells per digit, all cleared at the end. The value is kept.
- A single digit needs one scratch cell.
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| A value from 0 to 255 | about 260 + 34 per unit of x | about 14 + 9 per unit of x |
| Any cell size | about 270 + 41 per unit of x | about 30 + 8 per unit of x |
| One digit | 82 | 3 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| A value from 0 to 255 | x = 0 | 8 | 204 | 264 | 14 |
| A value from 0 to 255 | x = 7 | 8 | 204 | 439 | 70 |
| A value from 0 to 255 | x = 173 | 8 | 204 | 6,188 | 1,566 |
| A value from 0 to 255 | x = 255 | 8 | 204 | 8,844 | 2,296 |
| Any cell size | x = 0 | 8 | 160 | 272 | 31 |
| Any cell size | x = 173 | 8 | 160 | 7,403 | 1,440 |
| Any cell size | x = 255 | 8 | 160 | 10,554 | 2,095 |
| One digit | x = 7 | 8 | 22 | 82 | 3 |
Other forms
Any cell size
Loops until the quotient is zero. Layout: value, zero, then working cells.
>[-]>[-]+>[-]+<[>[-<-<<[->+>+<<]>[-<+>]>>]++++++++++>[-]+>[-]>[-]>[-]<<<<<[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>>-[-<<+>>]<[-]++++++++[-<++++++>]>>[-<<+>>]<<]<[.[-]<]<
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.
>++++++[-<++++++++>]<.
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
- Any program that shows a result.
- The chat program never prints numbers: it prints the bytes of word pieces directly.
Sources
- Source: Stack Overflow: "Printing a number in brainfuck", answer by itchyny: the 8-bit form, tested here for all 256 values.
- Source: Esolang wiki: Brainfuck algorithms: sections "Print value of cell x as number (8-bit)" and "… for ANY sized cell".
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.