2

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.

Rararat
  • 53
  • 6

2 Answers2

2

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/

ben rapoport
  • 101
  • 5
0

Most implementations of brainfuck will roll over from 0 to 255, and 255 to 0. Many programs will not work at all if they can't roll over.

In your program example, the ';' is not a command. It should probably be commas.

bf17
  • 1
  • 1