bgrag bgrag - 2 months ago 6x
Brainfuck Question

Infinite Counting Loop in Brainfuck

As a debugger/test program for my brainf*ck implementation, I have created the following counting loop:


With single byte, wrapping cells and with 30k cells with wraparound, this creates an infinite counting loop. It sets each cell to 1, then each cell to 2, etc..

My issue is, when it gets to 255, the inner loop increments the cell to 0, and then the outer loop increments it to 1 immediately. This acts as a 'double step' when it overflows, instead of a single step. This is the only way I could get the loop to continue infinitely with this tight of a program.

The count for each cell goes
0 -> 1 -> 2 ... 254 -> 255 -> 1 ...

While I would like it to go
0 -> 1 -> 2 ... 254 -> 255 -> 0 -> 1 ...

It is just hard to continue a loop when the current cell is zero. I have played with variations on the same theme, but none of them get it just right.

This particular program is just a trivial counting program for testing, but what will really help me with future programs is knowing how to invert the looping conditional like I need to to make this counting loop work.




After the first inner loop we know the data is 0 followed by a bunch of 255's. Move to the next cell and increment them all until we get back to the start. Then increment the first cell and start all over.