Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Comparators

Comparators are combinational digital circuits that tell whether two binary numbers are equal or which one is larger; they are used in ALUs, sorting, address comparison and control logic.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Comparators are used whenever a digital system must decide ordering or equality of binary values — for example inside an ALU to decide conditional branches, in address comparators for caches and memory controllers, and in digital control circuits for limit checking. Understanding comparators helps design fast, cascaded multi-bit comparators and to avoid errors when comparing signed vs unsigned numbers.

Key ideas

  • 1-bit comparator (exact derivation): Let inputs be A and B (single bits). Truth table (draw and label columns A, B, A>B, A<B, A=B). From truth table derive:

    • A>B occurs when A=1 and B=0, so A>B = A · B'.
    • A<B occurs when A=0 and B=1, so A<B = A' · B.
    • A=B occurs when both bits equal (both 0 or both 1); boolean function is A=B = A·B + A'·B'. This boolean function is called XNOR (exclusive-NOR) and is often written A ⊙ B or XNOR(A,B).

    To draw the diagram for a 1-bit comparator: draw two input wires A and B entering a small block labelled "1-bit comparator" with three output wires labelled A>B, A<B, A=B. Internally, show an AND gate with A and B' for A>B, an AND gate with A' and B for A<B, and an OR of (A·B) and (A'·B') for A=B (or a single XNOR gate). Label the inverted inputs where needed.

  • 2-bit comparator (step-by-step derivation): Let A = A1 A0 and B = B1 B0 (A1,B1 are MSBs). A is greater than B when either:

    1. MSB of A is greater than MSB of B (A1=1, B1=0), or
    2. MSBs are equal and LSB of A is greater than LSB of B.

    Express MSB equality as XNOR: E1 = A1·B1 + A1'·B1' = A1 ⊙ B1. Then derive:

    • A>B = A1·B1' + E1·(A0·B0')
    • A<B = A1'·B1 + E1·(A0'·B0)
    • A=B = E1 · (A0 ⊙ B0) = (A1 ⊙ B1) · (A0 ⊙ B0)

    For a diagram of 2-bit comparator: draw two 2-bit buses A1 A0 and B1 B0 into a block labelled "2-bit comparator". Inside show the MSB comparator feeding an equality signal E1. Show OR gate combining A1·B1' and E1·(A0·B0') to produce A>B, and analogous for A<B. Label gates and intermediate signals E1, A1·B1', A0·B0'.

  • General n-bit cascaded comparator (derivation): For bits indexed i = n−1 down to 0, define per-bit signals:

    • G_i (generate greater) = A_i · B_i' (means bit i alone makes A greater)
    • L_i (generate less) = A_i' · B_i
    • E_i (equality at bit i) = A_i ⊙ B_i = A_i·B_i + A_i'·B_i'

    Then the combined outputs for n bits (compare A_{n-1}...A_0 with B_{n-1}...B_0) are:

    • A>B = G_{n-1} + E_{n-1}·G_{n-2} + E_{n-1}·E_{n-2}·G_{n-3} + ... + E_{n-1}·...·E_1·G_0
    • A<B = L_{n-1} + E_{n-1}·L_{n-2} + E_{n-1}·E_{n-2}·L_{n-3} + ... + E_{n-1}·...·E_1·L_0
    • A=B = E_{n-1}·E_{n-2}·...·E_0

    Interpretation: test MSB first; if equal, test next bit, and so on. This form is what exam answers expect and is the basis of cascaded comparator ICs, which provide cascade inputs/outputs (I(A>B), I(A=B), I(A<B)) for chaining stages.

  • Signed numbers: If A and B are in two's complement (signed), simple unsigned comparator logic is not enough because MSB is sign bit. For signed comparison, handle sign bits specially: if signs differ, the positive number is larger; if signs same, compare magnitudes of remaining bits. State this requirement and show exam sketch: compare sign bits first, if A_sign=0 and B_sign=1 => A>B; if equal, use unsigned comparator on remaining bits. Alternative implementation using subtraction: for unsigned numbers, A ≥ B if and only if A − B produces no borrow (equivalently, when implemented as A + two's-complement of B, the carry-out is 1); for signed two's-complement, subtraction-based comparison must account for overflow, so the dedicated signed-compare approach above is preferred in exam answers.

  • Practical ICs: Many cascaded 4-bit comparator ICs exist (commonly used in labs) — a standard example is the 74HC85/74LS85 4-bit magnitude comparator. It provides three primary outputs A>B, A=B, A<B and three cascade inputs I(A>B), I(A=B), I(A<B) to build wider comparators: connect the lower-significance stage outputs to the higher-significance stage cascade inputs; for a single 4-bit comparison (no cascading), tie I(A=B)=1 and I(A>B)=I(A<B)=0.

Formulas

A>B (1-bit) = A · B' — A and B are input bits; apostrophe ' means NOT; unit: Boolean (logic 1/0); applies for single-bit unsigned comparison.

A<B (1-bit) = A' · B — same symbols and unit as above.

A=B (1-bit) = A·B + A'·B' = A ⊙ B — XNOR (A equals B); unit: Boolean; applies for single bits.

G_i = A_i · B_i' — per-bit generate-greater signal for bit i; unit: Boolean; used in cascade formulas.

L_i = A_i' · B_i — per-bit generate-less signal for bit i; unit: Boolean.

E_i = A_i ⊙ B_i = A_i·B_i + A_i'·B_i' — per-bit equality; unit: Boolean.

A>B (n-bit) = G_{n-1} + E_{n-1}·G_{n-2} + E_{n-1}·E_{n-2}·G_{n-3} + ... + E_{n-1}·...·E_1·G_0 — applies to unsigned n-bit numbers; unit: Boolean output.

A<B (n-bit) = L_{n-1} + E_{n-1}·L_{n-2} + ... + E_{n-1}·...·E_1·L_0 — unsigned n-bit.

A=B (n-bit) = E_{n-1}·E_{n-2}·...·E_0 — returns 1 only if all bit equalities hold.

Note: '·' means logical AND, '+' means logical OR, ' denotes logical NOT. There is no SI physical unit — signals are Boolean (represented by logic voltage levels).

Worked examples

Example 1 — 1-bit comparator (university short answer): Inputs: A=1, B=0. Step 1: Compute A>B = A·B' = 1 · (0)' = 1 · 1 = 1. Step 2: Compute A<B = A'·B = 0 · 0 = 0. Step 3: Compute A=B = A·B + A'·B' = 1·0 + 0·1 = 0 + 0 = 0. Answer: A>B=1, A<B=0, A=B=0.

Example 2 — 2-bit comparator numeric (exam-style): Compare A = 10₂ (A1=1, A0=0 => decimal 2) with B = 01₂ (B1=0, B0=1 => decimal 1). Step 1: Compute E1 = A1 ⊙ B1 = (1·0 + 0·1) = 0. Step 2: Compute G1 = A1·B1' = 1·1 = 1; L1 = A1'·B1 = 0·0 = 0. Step 3: Using 2-bit formula A>B = G1 + E1·(A0·B0') = 1 + 0·(...) = 1. Similarly A<B = L1 + E1·(...) = 0. A=B = E1·(A0 ⊙ B0) = 0. Answer: **A>B=1, A<B=0, A=B=0** (correct since 2 > 1).

Example 3 — 4-bit cascade trace (short trace): Compare A=0110₂ (6) and B=0101₂ (5). Bits: i=3..0. Compute per-bit from MSB (i=3): A3=0,B3=0 => E3=1,G3=0,L3=0. i=2: A2=1,B2=1 => E2=1,G2=0,L2=0. i=1: A1=1,B1=0 => E1=0,G1=1,L1=0. i=0: A0=0,B0=1 => E0=0,G0=0,L0=1. Now A>B = G3 + E3·G2 + E3·E2·G1 + E3·E2·E1·G0 = 0 + 1·0 + 1·1·1 + ... = G1 = 1. A=B = E3·E2·E1·E0 = 1·1·0·0 = 0. Answer: **A>B=1, A<B=0, A=B=0** (6 > 5).

Common mistakes

  • Forgetting to cascade equality signals (E_i) when building n-bit comparator from 1-bit blocks.
  • Applying unsigned comparator formulas directly to two's-complement signed numbers without special sign handling.
  • Mis-labelling bit indices: MSB index must be the highest index when writing the cascade formula (n−1 down to 0).

For your exam

Typical question types:

  • Definition/short note: define comparator, XNOR, MSB/LSB, difference between signed and unsigned comparison.
  • Derivation: derive expressions for a 1-bit and a 2-bit comparator and show the boolean simplification.
  • Diagram question: draw a 2-bit comparator block and label internal signals (E1, G1, etc.).
  • Numerical/trace: given A and B (n-bit), compute A>B, A<B, A=B using the cascade formulas.
  • Design: build an n-bit comparator using 1-bit blocks or using cascaded 4-bit comparator ICs.

What a full-marks answer includes:

  • Clear statement of assumptions (unsigned or signed numbers).
  • Correct truth table for 1-bit comparator or correct per-bit definitions G_i, L_i, E_i.
  • Step-by-step derivation for 2-bit or n-bit formulas showing MSB-first logic.
  • Properly labelled diagram when asked (inputs, outputs, gate labels, intermediate signals).
  • For signed comparison questions, explicit handling of the sign bit and explanation why it differs from unsigned comparison.

Quick check

  1. For 1-bit inputs A=0, B=1, what are A>B, A<B, A=B?
  2. Write the formula for A>B for a 2-bit comparator with bits A1 A0 and B1 B0.
  3. For n bits, what is the boolean expression that must be true for A=B?

Answers: 1) A>B=0, A<B=1, A=B=0. 2) A>B = A1·B1' + (A1 ⊙ B1)·A0·B0'. 3) A=B = E_{n-1}·E_{n-2}·...·E_0 (each E_i = A_i ⊙ B_i).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?