In the last tutorial, we saw how to use a 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:
loading…
Let’s break this down:
- The xor
checks each pair of digits. If they’re different, it emits
through its central channel; if they’re the same, it emits
.
- If both digits were 1, we now have
we need to dispose of coming out of the xor
. We use a distributor
and a crossing
to move them over to the right, where they’re out of the way.
- We now route the xor
central output to the trap
. The first
(indicating the first time the digits were different) will get held in the trap.
- Any subsequent
will pass straight through the trap and be ignored.
- Every 4 steps, the timer sends
into the trap. If there’s
trapped there, it gets released. If not, the trap just emits
.
- Therefore, every 4 steps, the trap emits
if at least one pair of digits was different, and
if they were all the same.
- Therefore, if
is emitted, the numbers are not equal. If
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