Getting started with BrainFuck

DPAmar
48.6K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Subtract numbers

Like we did for addition : in order to compute A-B, we have to subtract 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