equality checker

In the last tutorial, we saw how to use a trap trap to build a zero checker. The zero checker releases a marble only if the number in its input stream is non-zero.

Let’s now modify it so that instead of checking for zero, it checks if two numbers are equal:

00/0000/00
loading…

equality checker

Let’s break this down:

  • The xor xor checks each pair of digits. If they’re different, it emits marble through its central channel; if they’re the same, it emits hole.
  • If both digits were 1, we now have marblemarble we need to dispose of coming out of the xor xor. We use a distributor distributor and a crossing crossing to move them over to the right, where they’re out of the way.
  • We now route the xor xor central output to the trap trap. The first marble (indicating the first time the digits were different) will get held in the trap.
  • Any subsequent marble will pass straight through the trap and be ignored.
  • Every 4 steps, the timer sends marble into the trap. If there’s marble trapped there, it gets released. If not, the trap just emits hole.
  • Therefore, every 4 steps, the trap emits marble if at least one pair of digits was different, and hole if they were all the same.
  • Therefore, if marble is emitted, the numbers are not equal. If hole is emitted, they’re the same.

next

The ability to compare numbers will be a crucial component in any computer we build. For now, let’s get back to state. In the next tutorial, we’ll look at storing larger blocks of data efficiently.

continue