Search code examples
brainfuck

Brainfuck try to - in a 0 cell


What happens in Brainfuck if I try to "-" in a cell that contains a 0?

>-

Also, what happens if I try to start a loop while pointing to a 0 cell?

>[]

Edit: I forgot to mention it, but I'm trying to make an interpreter. Here's a piece of code I was given to use as an example:

;>;<[->++<][->++<]

In the second loop, the cell pointed at is "0", so my program starts an infinite loop.


Solution

  • Brainfuck is a very implementation dependent language. Usually, Brainfuck cells hold values ranging from 0-255, so 8 bit unsigned integer values. If you try to decrement a cell with a value of zero, as usually happens with computers you will perform an "underflow", meaning the value will go from 0 to 255. Similarly, if you try to increment a cell with a value of 255 - you will get 0.

    Looping in brainfuck can be though of with this piece of pseudo code: [SOME_CODE] is like while(*ptr!=0){SOME_CODE}

    So long as you are not pointing at a cell with a value of 0 in the beginning of the loop, you run the code within the brackets, and repeat.

    I suggest you take a look at https://fatiherikli.github.io/brainfuck-visualizer/