Search

mercredi 8 février 2017

Evaluate EQ / NEQ

Context

To know whether 2 values A and B are equal or not, we need to compute A - B: if the result is 0, then A=B, otherwise A<>B.
So, we can say that "A<>B" = (A-B).
Optionally, we can use some extra code to have evaluate A=B

Initial state

  • Memory: A, B
  • Cursor: first cell
  • Input: any

Process

  • While second cell is not null
    • Decrease first and second cells by 1
    • Loop invariant: first cell - second cell = A - B
    • When second cell equals 0, first cell equals A - B
  • Move to first cell

Code 

>[-<->]<

Final state

  • Memory: A <> B, 0
  • Cursor: first cell
  • Input: any
  • Output: unchanged

Note: to compute A=B, use second cell as 'else' bit, if A <> B then reset result and else bit; and finally move else bit to result position
>[-<->]+<[>-<[-]]>[-<+>]<

Aucun commentaire:

Enregistrer un commentaire