Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Sum numbers
As you probably already noticed, there is no "sum 2 variables" in BF. Variables actually don't exist either :)
The +
command even does not "add" values : it simply increments current cell, so does the -
command that decrements current cell.
In order to compute A+B, we have to add 1 to A, B times
Let's start
- Memory: A, B
- Cursor: on B
- Input: any
Process
- while B is not null
- invariant: A cell + B cell = A+B
- decrease B
- increase A
- loop
- B cell = 0, A cell = A+B - 0 = A+B
Code
[ while B is not null
- decrease B
<+ go to A and increase
> go back to B
] loop
Minified version
[-<+>]
Final state
- Memory: A+B, 0
- Cursor: "0" cell
- Input: unchanged
- Output: unchanged
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content