Possible Duplicate:
How to calculate the sum of 2 numbers with BrainFuck
Do anyone know how to write a simple BF program that adds two one-digit numbers? I am new to the language and need some help to grab the concepts.
Possible Duplicate:
How to calculate the sum of 2 numbers with BrainFuck
Do anyone know how to write a simple BF program that adds two one-digit numbers? I am new to the language and need some help to grab the concepts.
If you have two cells having a value of 0 to 9 each you can just add one to the other. Suppose you have two cells A and B. A is at position 0 and B is at position 1. You can add B to A like this (Assume the pointer starts at A). I will set A to 4, B to 8 and then add B to A:
setting A and B
++++>++++++++
remember the pointer is at B now so we can add B to A like this
[<+>-]
and now the pointer is still at B but B contains 0 and A contains 12
If you want to have the user input those single digit numbers, then keep in mind that when you use a , character, the ASCII code of the character is put in the current cell. So you first need to subtract 48 from the number (48 is the ASCII code of the character '0'). Here is an example filling A and B with two characters from the keyboard (I will assume that the user ONLY presses any of the number keys, and not letters or symbols)
Pointer starts at A so we have the user press a number key
,
we then subtract 48 from it so that it contains the actual value
------------------------------------------------
we move to B and do the same
>,------------------------------------------------
from here on it's the same as the last example
[<+>-]