Section 1.1
Incrementing, Decrementing, and Printing
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.
Code | Symbol | Code | Symbol | Code | Symbol |
---|---|---|---|---|---|
32 | (space) | 108 | l | 184 | ¸ |
33 | ! | 109 | m | 185 | ¹ |
34 | ” | 110 | n | 186 | º |
35 | # | 111 | o | 187 | » |
36 | $ | 112 | p | 188 | ¼ |
37 | % | 113 | q | 189 | ½ |
38 | & | 114 | r | 190 | ¾ |
39 | ’ | 115 | s | 191 | ¿ |
40 | ( | 116 | t | 192 | À |
41 | ) | 117 | u | 193 | Á |
42 | * | 118 | v | 194 | Â |
43 | + | 119 | w | 195 | Ã |
44 | , | 120 | x | 196 | Ä |
45 | - | 121 | y | 197 | Å |
46 | . | 122 | z | 198 | Æ |
47 | / | 123 | { | 199 | Ç |
48 | 0 | 124 | | | 200 | È |
49 | 1 | 125 | } | 201 | É |
50 | 2 | 126 | ~ | 202 | Ê |
51 | 3 | 127 | 203 | Ë | |
52 | 4 | 128 | € | 204 | Ì |
53 | 5 | 129 | 205 | Í | |
54 | 6 | 130 | ‚ | 206 | Î |
55 | 7 | 131 | ƒ | 207 | Ï |
56 | 8 | 132 | „ | 208 | Ð |
57 | 9 | 133 | … | 209 | Ñ |
58 | : | 134 | † | 210 | Ò |
59 | ; | 135 | ‡ | 211 | Ó |
60 | < | 136 | ˆ | 212 | Ô |
61 | = | 137 | ‰ | 213 | Õ |
62 | > | 138 | Š | 214 | Ö |
63 | ? | 139 | ‹ | 215 | × |
64 | @ | 140 | Œ | 216 | Ø |
65 | A | 141 | 217 | Ù | |
66 | B | 142 | Ž | 218 | Ú |
67 | C | 143 | 219 | Û | |
68 | D | 144 | 220 | Ü | |
69 | E | 145 | ‘ | 221 | Ý |
70 | F | 146 | ’ | 222 | Þ |
71 | G | 147 | “ | 223 | ß |
72 | H | 148 | ” | 224 | à |
73 | I | 149 | • | 225 | á |
74 | J | 150 | – | 226 | â |
75 | K | 151 | — | 227 | ã |
76 | L | 152 | ˜ | 228 | ä |
77 | M | 153 | ™ | 229 | å |
78 | N | 154 | š | 230 | æ |
79 | O | 155 | › | 231 | ç |
80 | P | 156 | œ | 232 | è |
81 | Q | 157 | 233 | é | |
82 | R | 158 | ž | 234 | ê |
83 | S | 159 | Ÿ | 235 | ë |
84 | T | 160 | 236 | ì | |
85 | U | 161 | ¡ | 237 | í |
86 | V | 162 | ¢ | 238 | î |
87 | W | 163 | £ | 239 | ï |
88 | X | 164 | ¤ | 240 | ð |
89 | Y | 165 | ¥ | 241 | ñ |
90 | Z | 166 | ¦ | 242 | ò |
91 | [ | 167 | § | 243 | ó |
92 | \ | 168 | ¨ | 244 | ô |
93 | ] | 169 | © | 245 | õ |
94 | ^ | 170 | ª | 246 | ö |
95 | _ | 171 | « | 247 | ÷ |
96 | ` | 172 | ¬ | 248 | ø |
97 | a | 173 | 249 | ù | |
98 | b | 174 | ® | 250 | ú |
99 | c | 175 | ¯ | 251 | û |
100 | d | 176 | ° | 252 | ü |
101 | e | 177 | ± | 253 | ý |
102 | f | 178 | ² | 254 | þ |
103 | g | 179 | ³ | 255 | ÿ |
104 | h | 180 | ´ | ||
105 | i | 181 | µ | ||
106 | j | 182 | ¶ | ||
107 | k | 183 | · |
According to the table, the ASCII code for the character H
is 72. We’ll use the following logic to print out the character:
- Increment the current cell
- Repeat previous step 72 times such that the current cell stores 72
- 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:
- 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 characterH
- 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 charactere
- 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 characterl
- To print out a second
l
(108): 1. Print out the ASCII characterl
- 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 charactero
- 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 ` ` - 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 characterW
- 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 charactero
- 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 characterr
- 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 characterl
- 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 characterd
- 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.