# In minecraft, how do I construct a 4-bit comparator?

4-bit comparators are a basic building block in electronics, and useful in many redstone devices, in particular computers. I'm interested in a good overview of the construction this type of device. It takes eight signals as input: `a0, a1, a2, a3, b0, b1, b2, b3`; and outputs 2 signals: `a<b, a=b` with `a>b = !(a<b)`.

Diagrams of how to do it with real electronics are easy to find. Usually people build 1-bit, 2-bit, and 4-bit comparators directly, and larger ones are created through cascades (to minimize the number of transistors used). Translating this to minecraft is a bit of an art; sometimes a more complicated circuit in reality is easier to do (and works better) in minecraft. There's these points to consider, roughly in order of importance:

• Maintainability

The plan should be for the device to remain functioning as the game is updated. Answers using bugs or undefined behaviour would result in the device no longer working as the game updates, making these less useful.

• Speed

There's usually an expectation that whatever redstone contraption is providing the logic functions fast enough that the delay isn't noticeable.

• Simplicity

If the device is meant as a tutorial, it should be easy enough to understand, and not rely on too much obscure behaviour.

• Compactness

A device using a minimal amount of material is quicker to build and easier to fit in.

## 1 Answer

While it's possible to build a contraption that operates on discrete boolean arithmetics, built of logic gates, it would be terribly impractical; you have some 5 gates per bit, each gate a good couple blocks, cross-connections, this would be quite awful. Luckily, for arithmetics on numbers up to 4 bits, Minecraft provides a much handier and more graceful tool - analog redstone; use of signal strengths and the comparator.

This is the device that meets the requirements as stated (shown state: "15 > 14", green input greater than blue.):

It looks bulky, but that's only due to need to "massage" the problem as stated (binary input, outputs of "<,=") into what works naturally in Minecraft (analog inputs and outputs of "<,>"). If you take the Minecraft-natural approach all you need is the "core" of 2 comparators:

In this case, you deliver two signals of varying strength, X,Y. The outputs are their difference X-Y and Y-X if the result is positive; no output if zero or less.

What the "extended" version does is first convert 4 input bits into analog signal (through subtracting 1,2,4 or 8 respectively; in Minecraft analog circuitry all addition is done as `a + b = 15 - (15 - a - b)`)*, then compared; then the two outputs are fed downwards into a NOR gate, to produce output if neither is active, meaning the inputs are equal. If you skip the repeaters on output, your output will contain magnitude of the difference too.

*The inputs aren't normalized back to the original value `15 - (15 - x)`, just fed into the comparator in the 15-complement form `15 - x`, as this compacts the contraption; instead of reversing the values, I'm just flipping the meaning of the outputs; if x < y, then (15 - x) > (15 - y)

• Redstone logic like this always blows my mind.
– Ben
Oct 23, 2020 at 4:04