Getting started with BrainFuck
Copying a value is a destructive operation. The initial value is deleted while 2 copies are created.
Optionally, an extra "Move" operation can be used to move one of the 2 copies at source location, to create a "conservative copy illusion".
- Memory: A, 0, 0
- Cursor: on A
- Input: any
- while A is not null
- invariant: A cell + 1s next cell = A, and 1st next cell = 2nd next cell
- decrease A
- increase 1st next cell
- increase 2nd next cell
- A cell = 0, 1st next cell = A - 0 = A and 2nd next cell = 1st next cell = A
[ while A is not null - decrease A >+ increase 1st next cell >+ increase 2nd next cell << go back to A ] loop
- Memory: 0, A, A
- Cursor: "0" cell
- Input: unchanged
- Output: unchanged
As explained, we can move the 2nd copy to source location to have A, A, 0 instead of 0, A, A
[->+>+<<] copy value >>[-<<+>>]<< go to 2nd copy; move it to source location; then go back to source location
, read a char [->+>+<<] copy value >. go to 1st copy and print >. go to 2nd copy and print
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.