Lesson 11 +30 XP

Magnitude Comparator: 1-bit, 2-bit, 4-bit & Cascading

Magnitude Comparator: 1-bit, 2-bit, 4-bit & Cascading

A magnitude comparator compares two unsigned binary numbers A and B and tells whether A > B, A = B or A < B. Exactly one of its three outputs is 1 at any time. Comparators are used in CPUs for conditional branches, in sorting hardware, in address decoding and in control systems ("is the temperature above the set point?").

1. One-Bit Comparator

ABA > BA = BA < B
00010
01001
10100
11010
(A > B) = AB' qquad (A < B) = A'B qquad (A = B) = A'B' + AB = (A oplus B)' = A odot B

The equality output is the XNOR (equivalence) of the two bits. Note that (A = B) = big((A > B) + (A < B)big)', so one NOR gate can produce it from the other two outputs.

One-bit comparatorOne-bit comparator

2. Two-Bit Comparator

Inputs A = A1 A0 and B = B1 B0: 4 inputs, 16 rows. The rows where A > B are (A, B) = (01, 00), (10, 00), (10, 01), (11, 00), (11, 01), (11, 10).

Ordering the input variables as A1 A0 B1 B0 gives minterm lists and K-map results:

OutputMintermsMinimal SOP
A > B4, 8, 9, 12, 13, 14A1 B1' + A0 B1' B0' + A1 A0 B0'
A < B1, 2, 3, 6, 7, 11A1' B1 + A1' A0' B0 + A0' B1 B0
A = B0, 5, 10, 15(A1 odot B1)(A0 odot B0)

The A = B map has four isolated 1s (no grouping is possible), which is why it is written with XNOR gates instead of as an SOP.

K-maps of the 2-bit comparatorK-maps of the 2-bit comparator

Reading the A > B expression in words: A is bigger if its MSB is 1 while B's MSB is 0 (A1 B1'), or the MSBs are equal and A0 = 1, B0 = 0. The terms A0 B1' B0' and A1 A0 B0' are the K-map's way of writing "MSBs equal and A0 > B0" in two cases.

3. Four-Bit Comparator: The Algorithm

A 4-bit comparator has 8 inputs and 256 rows, so a truth table is impractical. Instead, use the way people compare numbers: start at the most significant bit.

  1. Define the equality of each bit pair: xi = Ai Bi + Ai' Bi' = (Ai oplus Bi)'.
  2. A = B when all bit pairs are equal.
  3. A > B when, at the first position (from the left) where the bits differ, A has the 1.
  4. A < B when, at that position, B has the 1.
(A = B) = x3 x2 x1 x0
(A > B) = A3 B3' + x3 A2 B2' + x3 x2 A1 B1' + x3 x2 x1 A0 B0'
(A < B) = A3' B3 + x3 A2' B2 + x3 x2 A1' B1 + x3 x2 x1 A0' B0

4-bit magnitude comparator logic4-bit magnitude comparator logic

SolvedExample 1 · Tracing the 4-bit comparator

Find the outputs for A = 1011 and B = 1001.

  1. Equality bits: x3 = (1 odot 1) = 1, x2 = (0 odot 0) = 1, x1 = (1 odot 0) = 0, x0 = (1 odot 1) = 1.
  2. (A = B) = x3 x2 x1 x0 = 0.
  3. (A > B): term 1 A3 B3' = 1 · 0 = 0; term 2 x3 A2 B2' = 1 · 0 · 1 = 0; term 3 x3 x2 A1 B1' = 1 · 1 · 1 · 1 = 1. So (A > B) = 1.
  4. (A < B): every term is 0 (the first difference, at bit 1, favours A).
AnswerA > B = 1, A = B = 0, A < B = 0 (11 > 9)

Gate count: four XNOR gates for x3 ldots x0, one 4-input AND for equality, and for each inequality output four AND gates (of 2, 3, 4 and 5 inputs) and a 4-input OR, plus inverters. The circuit has only three levels of logic regardless of the input values, so it is fast.

The comparator in Verilog

This model codes the three equations directly and the testbench checks them against Verilog's own >, == and < operators for all 256 input pairs:

VERILOG
// 4-bit magnitude comparator from the textbook equations, checked against Verilog's operators
module comp4 (
    input  [3:0] a, b,
    output       gt, eq, lt
);
    wire [3:0] x = ~(a ^ b);   // xi = 1 when bit i of A equals bit i of B
    assign eq = &x;
    assign gt = (a[3] & ~b[3])
              | (x[3] & a[2] & ~b[2])
              | (x[3] & x[2] & a[1] & ~b[1])
              | (x[3] & x[2] & x[1] & a[0] & ~b[0]);
    assign lt = (~a[3] & b[3])
              | (x[3] & ~a[2] & b[2])
              | (x[3] & x[2] & ~a[1] & b[1])
              | (x[3] & x[2] & x[1] & ~a[0] & b[0]);
endmodule

module tb;
    reg  [3:0] a, b;
    wire       gt, eq, lt;
    integer i, bad;

    comp4 dut (.a(a), .b(b), .gt(gt), .eq(eq), .lt(lt));

    initial begin
        a = 4'b1011; b = 4'b1001;
        #1 $display("A=%b B=%b  A>B=%b A=B=%b A<B=%b", a, b, gt, eq, lt);
        a = 4'b0110; b = 4'b0110;
        #1 $display("A=%b B=%b  A>B=%b A=B=%b A<B=%b", a, b, gt, eq, lt);
        bad = 0;
        for (i = 0; i < 256; i = i + 1) begin
            {a, b} = i;
            #1;
            if (gt !== (a > b) || eq !== (a == b) || lt !== (a < b)) bad = bad + 1;
        end
        $display("all 256 pairs checked, errors = %0d", bad);
    end
endmodule

The textbook equations agree with the built-in operators everywhere:

OUTPUT
A=1011 B=1001  A>B=1 A=B=0 A<B=0
A=0110 B=0110  A>B=0 A=B=1 A<B=0
all 256 pairs checked, errors = 0

The expression &x is a reduction AND: it ANDs all bits of x together, which is exactly x3 x2 x1 x0.

4. Cascading Comparators (IC 7485)

The IC 7485 is a 4-bit magnitude comparator with three extra cascade inputs IA>B, IA=B, IA<B. They carry the result of a less significant stage, and are used only when all four bits of this stage are equal:

(A > B)out = (A > B)_{this stage} + (A = B)_{this stage} · IA>B

and similarly for the other two outputs.

To compare 8-bit numbers, connect two 7485s:

  • the lower IC compares A3 ldots A0 with B3 ldots B0; its cascade inputs are tied to IA>B = 0, IA=B = 1, IA<B = 0 (as if "equal so far");
  • its three outputs feed the cascade inputs of the upper IC, which compares A7 ldots A4 with B7 ldots B4;
  • the upper IC's outputs are the final result.

Two 7485 comparators cascaded for 8-bit comparisonTwo 7485 comparators cascaded for 8-bit comparison

SolvedExample 2 · Cascaded comparison

With two cascaded 7485s, compare A = 0110 1001 and B = 0110 0111.

  1. Lower IC: 1001 vs 0111. The MSB differs and favours A: outputs (>, =, <) = (1, 0, 0).
  2. Upper IC: 0110 vs 0110 are equal, so it passes its cascade inputs through: (1, 0, 0).
AnswerA > B (105 > 103)

5. Comparing Signed Numbers

The circuits above treat A and B as unsigned. For 2's complement numbers, the sign bit has negative weight, so a number with sign bit 1 is smaller. A simple fix is to invert both sign bits before an unsigned comparison (this maps -8 ldots +7 onto 0 ldots 15 in the same order). A CPU usually compares by subtracting A - B and inspecting the flags: Z = 1 means equal; for unsigned numbers the carry flag, and for signed numbers N oplus V, tells which is smaller.

💡 Tip

Key points to remember

  • 1-bit: A > B = AB', A < B = A'B, A = B = (A oplus B)'.
  • n-bit equality: AND of all xi = (Ai oplus Bi)'.
  • A > B: first unequal bit from the MSB has Ai = 1: A3 B3' + x3 A2 B2' + x3 x2 A1 B1' + x3 x2 x1 A0 B0'.
  • 7485 cascading: lower stage's outputs feed the next stage's I inputs; the first stage gets IA=B = 1.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Design a 2-bit magnitude comparator. Give the truth table, K-maps and logic diagram. (10 Marks)
  • Q2. Derive the logic expressions of a 4-bit magnitude comparator and draw its circuit. (10 Marks)
  • Write: xi definition, the three output equations, and the diagram with XNOR and AND-OR gates.
  • Q3. How are two 4-bit comparators (7485) cascaded to compare 8-bit numbers? (5 Marks)
  • Q4. Trace the 4-bit comparator for A = 1011 and B = 1001. (2.5 Marks)