Question

I am writing Brainfuck interpreter in Python and I'm currently testing this code:

,>++++++[<-------->-],[<+>-]<.

with this input:

43

Polish Wikipedia said the result should be 7, but something is not right here. How should I treat the input? As 4 and 3 or as: '4' and '3' (52 and 51)? If I treat input characters as integers then result is -41, because after first loop [0] is -44 and [1] is 0, then [1] becomes 3 and we add that 3 to -44 so it results in -41.

On the other hand if I treat input characters as ASCII codes then after the first loop [0] is 4 and [1] is 0, then [1] becomes 51 and we add that 51 to 4 so it results in 55. (this JavaScript interpreter also gives result 55: https://www.nayuki.io/page/brainfuck-interpreter-javascript)

However, I downloaded some interpreter written in C and it gives result 7, the same as Wikipedia told... so I am confused. There is that interpreter: http://pastebin.com/aasxLppa , also this interpreter gives result 7: brainfuck.tk -.-

Was it helpful?

Solution

The inputs are 4 and 3, as bytes. The input is taken as ASCII characters, the values are thus 52 and 51.

  • The first input is decremented by 8, 6 times, so 52 - 48 = 4.

  • The second input is added to the first input, so 4 plus 51 = 55.

  • 55 is written to the output, and that is ASCII 7.

The program sums up two ASCII digits.

The JavaScript interpreter is entirely correct. You were looking at the first register, which holds the byte value of 55, but the output box shows 7, the ASCII character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top