Link Search Menu Expand Document

Section 1.1

Incrementing, Decrementing, and Printing

← Previous Next →

Brainfuck operates by simulating a pure Turing machine, in which the user controls a pointer to manipulate the values of cells on a pseudo-infinite tape. The ‘pointer’ is a digital object that allows the user to interact with a certain cell. All values in the tape begin at ‘blank’ status, or 0.

A blank initialized tape.

...[0][0][0][0][0][0][0]...
             ^

To increment the value of the cell that is being pointed to, we use the increment command +.

The result of executing the command +.

...[0][0][0][1][0][0][0]...
             ^

Similarly, to decrement the value of the cell that is being pointed to, we use the decrement command -. We can undo the effect of incrementing by immediately following with a decrement command.

The result of executing the command +-.

...[0][0][0][0][0][0][0]...
             ^

To produce output, Brainfuck uses the ASCII protocol to map integers to characters. The . command prints out the ASCII character corresponding to the value being stored in the current cell being pointed to. For this reason, it is helpful to keep an ASCII table on hand. It is printed below and on the Reference page.

CodeSymbolCodeSymbolCodeSymbol
32(space)108l184¸
33!109m185¹
34110n186º
35#111o187»
36$112p188¼
37%113q189½
38&114r190¾
39115s191¿
40(116t192À
41)117u193Á
42*118v194Â
43+119w195Ã
44,120x196Ä
45-121y197Å
46.122z198Æ
47/123{199Ç
480124|200È
491125}201É
502126~202Ê
513127 203Ë
524128204Ì
535129 205Í
546130206Î
557131ƒ207Ï
568132208Ð
579133209Ñ
58:134210Ò
59;135211Ó
60<136ˆ212Ô
61=137213Õ
62>138Š214Ö
63?139215×
64@140Œ216Ø
65A141 217Ù
66B142Ž218Ú
67C143 219Û
68D144 220Ü
69E145221Ý
70F146222Þ
71G147223ß
72H148224à
73I149225á
74J150226â
75K151227ã
76L152˜228ä
77M153229å
78N154š230æ
79O155231ç
80P156œ232è
81Q157 233é
82R158ž234ê
83S159Ÿ235ë
84T160 236ì
85U161¡237í
86V162¢238î
87W163£239ï
88X164¤240ð
89Y165¥241ñ
90Z166¦242ò
91[167§243ó
92\168¨244ô
93]169©245õ
94^170ª246ö
95_171«247÷
96`172¬248ø
97a173 249ù
98b174®250ú
99c175¯251û
100d176°252ü
101e177±253ý
102f178²254þ
103g179³255ÿ
104h180´  
105i181µ  
106j182  
107k183·  

According to the table, the ASCII code for the character H is 72. We’ll use the following logic to print out the character:

  1. Increment the current cell
  2. Repeat previous step 72 times such that the current cell stores 72
  3. Print out the ASCII character corresponding to the value in the current cell

We can make our code more readable by breaking up our 72 increment commands into 7 groups of 10 and a remainder of 2. Brainfuck doesn’t interpret any characters beyond the eight commands (+, -, <, >, ., ,, [, and ]), so anything else - new lines, tabs, other characters - can be used as formatting tools or comments.

Printing out the symbol H.

Add 72 to the current cell
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++

Print out current value in cell
.

Let’s print out the rest of our Hello World! phrase simply by decrementing and incrementing the value in the cell to reach the corresponding ASCII code for each character. Here is our complete logic:

  1. To print out H (72): 1. Increment the current cell 2. Repeat the previous step 72 times such that the current cell stores 72 3. Print out the ASCII character H
  2. To print out e (101): 1. Increment the current cell 2. Repeat the previous step 29 times such that the current cell stores 101 3. Print out the ASCII character e
  3. To print out l (108): 1. Increment the current cell 2. Repeat the previous step 7 times such that the current cell stores 108 3. Print out the ASCII character l
  4. To print out a second l (108): 1. Print out the ASCII character l
  5. To print out o (111): 1. Increment the current cell 2. Repeat the previous step 3 times such that the current cell stores 111 3. Print out the ASCII character o
  6. To print out [space] (32): 1. Decrement the current cell 2. Repeat the previous step 79 times such that the current cell stores 32 3. Print out the ASCII character ` `
  7. To print out W (87): 1. Increment the current cell 2. Repeat the previous step 55 times such that the current cell stores 87 3. Print out the ASCII character W
  8. To print out o (111): 1. Increment the current cell 2. Repeat the previous step 24 times such that the current cell stores 111 3. Print out the ASCII character o
  9. To print out r (114): 1. Increment the current cell 2. Repeat the previous step 3 times such that the current cell stores 114 3. Print out the ASCII character r
  10. To print out l (108): 1. Decrement the current cell 2. Repeat the previous step 6 times such that the current cell stores 108 3. Print out the ASCII character l
  11. To print out d (100): 1. Decrement the current cell 2. Repeat the previous step 8 times such that the current cell stores 100 3. Print out the ASCII character d
  12. To print out ! (33): 1. Decrement the current cell 2. Repeat the previous step 67 times such that the current cell stores 33 3. Print out the ASCII character !

We can translate this to Brainfuck code:

Printing out h
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++
.

Printing out e
++++++++++
++++++++++
+++++++++
.

Printing out l
+++++++
..

Printig out o
+++
.

Printing out ' ' (space)
----------
----------
----------
----------
----------
----------
----------
---------
.

Printing out W
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
+++++
.

Printing out o
++++++++++
++++++++++
++++
.

Printing out r
+++
.

Printing out l
------
.

Printing out d
--------
.

Printing out !
----------
----------
----------
----------
----------
----------
-------
.

Phew! That was a lot of work to print out Hello World!. We’ll develop more efficient ways to perform the same task in later sections.

← Previous Next →