LearnBuild toward a chatbotThe machine

The machine

Aim. At the end of this lesson you can write a program that puts any number in any cell. You need nothing but eight characters.

Where the lessons are going

Every lesson adds one part to a single small sum. In lesson 11 one program does the whole sum and prints a word. This lesson adds the first part: getting a number, such as the 90 the sum starts with, into a cell.

A row of boxes and a pointer

Try it: a row of six boxes, a mark on one of them, and four buttons. The buttons call the mark the finger. Use them to put the number 3 in the box called "cell 1":

Figure 1. A tape of six cells and four buttons. Watch which cell the pointer is on before you add. (interactive)

A program runs by itself

Try it: the same row, now with a program already typed. Press Step until the program has ended. Before each press, read the line that starts with "Next":

Figure 2. A program of six commands. The lit-up command is the one that runs next; the line "Next" says what it will do. (interactive)

Write a number

Try it: type a program that leaves 12 in cell 2. You only need > and +:

Figure 3. A box for your own program. The tape below it shows where that program ends. (interactive)

Doing it again and again

Ninety plus signs is a lot of typing. This program repeats a few commands instead. It starts with a number in the left cell and ends with a number in the right one. Change what the left cell starts at and what is added each round, then press Play, or drag Step back and forth:

Every round took 1 from the left cell and added the same amount to the right cell, until the left cell was empty. With 3 and 4 that is three rounds, and the right cell ends at 12. Set the left cell to 0 and nothing is added at all. The sliders stop at 9 to keep the run short.

The square brackets are what make it repeat. They mean: repeat what stands between us until the cell under the pointer is zero. Such a repeat is called a loop.

Try it: this is the loop with 3 and 4. Press Step until the program has ended, and watch what the two brackets ask:

Figure 4. A loop. Watch cell 0 count down while cell 1 goes up by four each round. (interactive)

The same number, one cell further

Try it: this is the loop program of step 4. It leaves 12 in cell 1. Change it so that the 12 lands in cell 2. You may use 13 commands at most:

Figure 5. The loop program of step 4, for you to change. The line under the tape says what cell 2 holds at the end. (interactive)

Letters come out

Numbers can come out as letters. This program builds a number in cell 1 with a loop and then prints it. Change the number of rounds and read the line "Printed":

The last command, . (a full stop), printed the cell as one letter. A computer keeps every letter as a number, and each round here adds 8: eight rounds make 64, one more plus makes 65, and 65 is the number for A. Nine rounds give 73 and the letter I.

Try it: with eight rounds. Press "To the end". What does the program print?

Figure 6. A program that prints. Watch cell 1 reach 65, and the line "Printed so far". (interactive)

Below zero

A cell holds a whole number of a fixed size, and the size matters. This program only takes 1 from a cell that holds 0. Choose how many bits a cell has, and read what it holds afterwards:

The cell did not go to −1. It went to the highest number it can hold: 255 for 8 bits, 65,535 for 16 and 4,294,967,295 for 32. After the highest number comes 0 again, and before 0 comes the highest number, like the mileage counter of a car.

Try it: a cell of 8 bits holds 0. Press Step once to take 1 off:

Figure 7. One minus on an empty cell. Watch the value of cell 0. (interactive)

What you can do now

Put any number in any cell, repeat something with a loop, and print a letter. For the sum these lessons build toward, that is the first part: the 90 can stand in a cell.

To use the 90 in a sum, the program has to read it, and reading a cell turns out to empty it. What can be done about that?

The eight commands in one table

Table 1. The eight commands of brainfuck.
CommandWhat it does
+ −Add 1 to the cell under the pointer, or take 1 off.
> <Move the pointer one cell to the right or to the left.
[ ]Repeat everything between them until the cell under the pointer is zero.
,Take one letter of input and put its number in the cell.
.Print the cell as one letter.

On your keyboard the minus is the ordinary hyphen key. Anything in a program that is not one of these characters is skipped.

A cell that goes up to 255 has 8 bits; one that goes up to 4,294,967,295 has 32 bits. Step 1 of lesson 5 shows what a bit is.

This lesson shows the machine with six cells. The tape of the chat program has 188,012,789 of them.