LearnBuild toward a chatbotThe way through: peeling off bits

The way through: peeling off bits

Aim. At the end of this lesson your program divides 11,868 by 128, rounds it and keeps it within limits, in about 120 steps. In step 3 of lesson 4 that took 131,538.

Where the lessons are going

Counting costs a step for every unit of a number, so the division of the sum is stuck. The way out starts with something from step 7 of lesson 1: a cell that is full goes round to zero.

Push a number out of its cell

Try it: the cell holds 44, drawn as a row of eight switches. Press Double until the cell holds zero, and watch the switches:

Figure 1. The eight bits of one cell. Watch the bits move left, and the leftmost one fall off. (interactive)

A question about one bit

Try it: press the bits to make a number that is zero after exactly three doublings, not sooner. Then press Double three times:

Figure 2. The same cell, with bits you can set. The number must reach zero on the third doubling. (interactive)

Read a whole number, bit by bit

Try it: the number is 93. Press Play, or go to the last step. Then compare the cells called "bit" with the switches above the program:

Figure 3. The peel as a program. The cells called "bit" on the right fill in, lowest bit first. (interactive)

Dividing by 128 without counting

128 is seven doublings, so dividing by 128 means throwing away the lowest seven bits. To round at the same time, add 64 first. Drag the number and watch which bits are kept:

The bits are worked out in this page, not by a brainfuck program. The next step runs the real one.

Notice that the kept bits are always the number divided by 128 and rounded, and that they only change when the number has moved on by a whole 128. Throwing bits away is how a program divides without counting.

Counting against peeling

Try it: press "Run both" for 11,868. Then make the number ten times as large and run both again:

Divide by 128 and roundAnswerSteps
By counting (step 3 of lesson 4)––
By peeling––

Figure 4. Two programs for the same division, both run on your number. Compare the two step counts. (interactive)

Keep it within limits

Try it: drag the sum upward until the result stops growing:

Divided by 128 and rounded
Result of the brainfuck program
Steps
Figure 5. Divide, round and clamp as one program. The marker shows the rounded sum; the green band is what the clamp lets through. (interactive)

What you can do now

Divide, round and clamp, at a cost of about a hundred steps whatever the number. With that, comparing is solved too: subtract, and look at one bit. The first obstacle is gone.

So far every weight stood in the program (lesson 3). A model also multiplies two numbers that are both on the tape. How is that done?

How the real program differs

The peel of step 3 for your number, bit by bit: start with the lowest bit and double the number 7 times, so that only the lowest bit can survive. Zero or not? If the bit was on, take it out. Then the next bit, with 6 doublings, and so on. The table shows it for the number of step 3.

Table 1. The peel of your number, bit by bit (worked out by the page).

Two honest notes. Peeling leans on "a multiplying loop is one step" (step 3 of lesson 3). Done one command at a time, a single doubling loop on a large cell would run for millions of rounds, so the real program needs a fast interpreter. And the program text depends on the size of the cells: the chat program is written for cells of 32 bits and gives nonsense on cells of 8 bits.

The same peel makes other things. Looking a value up in a list of 256 ready-made answers takes about 68 steps, against about 500 by counting. Step 2 of lesson 10 uses that.