Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksBuild a CPUToolsOpen the lab →

8-bit magnitude comparator: GT, EQ and LT, live

An 8-bit magnitude comparator compares two bytes, A7..A0 and B7..B0, and raises exactly one of three outputs: GT when A is greater, EQ when they are equal, or LT when A is less. The answer is decided at the highest bit where A and B differ.

Comparing binary numbers works like comparing decimal ones: start at the most significant digit and stop at the first difference. For each bit the circuit computes e_i = A_i XNOR B_i (the bits are equal) and g_i = A_i AND NOT B_i (A has the 1). A is greater if bit 7 says so, or bits 7 are equal and bit 6 says so, and so on down: GT = g7 + e7 g6 + e7 e6 g5 + ... + e7 e6 e5 e4 e3 e2 e1 g0. A running AND of the e signals gives every "all higher bits equal" condition, and its last value is EQ. Exactly one output is 1, so LT is simply NOR(GT, EQ). The 4-bit magnitude comparator is the same circuit half as wide, and the 7485 chip does 4 bits with cascade inputs so two of them make an 8-bit comparator.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

Embed or cite this circuit

Free to embed on a course page, blog or worksheet: paste this code. The circuit stays live, and the small digiwleea badge links back here.

To cite it: digiwleea, “8-bit magnitude comparator”, https://digiwleea.wleeaf.dev/tools/build/comparator-8bit/

What you need

Step by step

  1. Place inputs A7 to A0 and B7 to B0, and probes GT, EQ and LT.
  2. For each bit, make e_i = A_i XNOR B_i and g_i = A_i AND NOT B_i.
  3. Chain ANDs of e7, e6, ... to get "all higher bits equal" for each position; AND each with that position's g_i and OR the results, starting with g7, to get GT. The last AND of the chain is EQ.
  4. LT = NOR(GT, EQ). Set two bytes and flip bits from the top down: the first difference decides.

Comparator examples

ABHighest differing bitGTEQLT
1001 0110 (150)1001 0110 (150)none010
1000 0000 (128)0111 1111 (127)7: A has the 1100
0110 0101 (101)0110 1000 (104)3: B has the 1001
1111 1110 (254)1111 1111 (255)0: B has the 1001

The full table has 2^16 rows, so before the page is built the circuit is checked on 1,377 of them: every value against itself, every one-bit difference in each direction over a spread of values, every pair of neighbours (A = B plus or minus 1) and 1,000 seeded random pairs.

You can also read the full theory in the lesson on comparators. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

More builds: Comparing numbers

All 246 build guides, by topic

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the 8-bit magnitude comparator in the lab →

Frequently asked

How does an 8 bit magnitude comparator work?

It compares the two numbers from the most significant bit down. The first bit where they differ decides: if A has the 1 there, A is greater; if B does, A is less; if no bit differs, they are equal.

How do you build an 8 bit comparator from 4 bit comparators?

Compare the high halves first and use the low halves only when the high halves are equal: GT = GT_high + EQ_high GT_low, EQ = EQ_high EQ_low, LT = LT_high + EQ_high LT_low. The 7485 chip has cascade inputs for exactly this.

What are the outputs of a magnitude comparator?

Three: A greater than B, A equal to B, and A less than B. Exactly one of them is 1 for any pair of inputs.