Design a 2-bit comparator

Design a 2-bit comparator. The comparator input has two 2-bit numbers A and B. A consists of 2 bits a1 and a0. B consists of 2 bits b1 and b0. The comparator has three outputs Z2, Z1, Z0. a) Show the truth table for this comparator b) Write the logic equation for Z2 that are true if only if A < b=”” c)=”” write=”” the=”” logic=”” equation=”” for=”” z1=”” that=”” are=”” true=”” if=”” only=”” if=”” a=””> B d) Write the logic equation for Z0 that are true if only if A = B e) Show the logic design for this comparator using the minimum number of AND, OR, and Inverters. f) Use Logisim to verify your design

The Correct Answer and Explanation is:


Problem Setup:

  • Inputs:
    • A = a1 a0 (2-bit number)
    • B = b1 b0 (2-bit number)
  • Outputs:
    • Z2 = 1 if A < B
    • Z1 = 1 if A > B
    • Z0 = 1 if A = B

a) Truth Table

| a1 | a0 | b1 | b0 || A | B | Z2 (A < B) | Z1 (A > B) | Z0 (A = B) |
|—-|—-|—-|—-||—-|—-|————–|————–|————–|
| 0 | 0 | 0 | 0 || 0 | 0 | 0 | 0 | 1 |
| 0 | 0 | 0 | 1 || 0 | 1 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 || 0 | 2 | 1 | 0 | 0 |
| 0 | 0 | 1 | 1 || 0 | 3 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 || 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 || 1 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 || 1 | 2 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 || 1 | 3 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 || 2 | 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 || 2 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 || 2 | 2 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 || 2 | 3 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 || 3 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 || 3 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 || 3 | 2 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 || 3 | 3 | 0 | 0 | 1 |


b) Logic Equation for Z2 (A < B)

Z2 = (¬a1 ∧ b1) ∨ (a1 ≡ b1) ∧ (¬a0 ∧ b0)

Using basic logic gates:

Z2 = (¬a1 ∧ b1) ∨ ((a1 ∧ b1) ∨ (¬a1 ∧ ¬b1)) ∧ (¬a0 ∧ b0)

c) Logic Equation for Z1 (A > B)

Z1 = (a1 ∧ ¬b1) ∨ (a1 ≡ b1) ∧ (a0 ∧ ¬b0)

Using basic gates:

Z1 = (a1 ∧ ¬b1) ∨ ((a1 ∧ b1) ∨ (¬a1 ∧ ¬b1)) ∧ (a0 ∧ ¬b0)

d) Logic Equation for Z0 (A = B)

Z0 = (a1 ≡ b1) ∧ (a0 ≡ b0)

Which simplifies to:

Z0 = (a1 ⊙ b1) ∧ (a0 ⊙ b0)  // XNOR gates

e) Logic Design Using Minimum Gates

  • XNOR Gate: Use 2 for Z0.
  • AND/OR Gates: For Z1 and Z2, use a mix of basic AND, OR, NOT gates.
  • Use gate sharing (e.g., use a1 ≡ b1 in both Z1 and Z2).

Diagram layout (for Logisim or circuit):

  1. Generate ¬a1, ¬a0, ¬b1, ¬b0
  2. Compute a1 ≡ b1, a0 ≡ b0
  3. Build Z0 = (a1 ≡ b1) ∧ (a0 ≡ b0)
  4. Compute:
    • Z2 = (¬a1 ∧ b1) ∨ ((a1 ≡ b1) ∧ (¬a0 ∧ b0))
    • Z1 = (a1 ∧ ¬b1) ∨ ((a1 ≡ b1) ∧ (a0 ∧ ¬b0))

f) Use Logisim to Verify

In Logisim:

  • Create inputs a1, a0, b1, b0
  • Use NOT, AND, OR, and XNOR gates as above
  • Display outputs Z2, Z1, Z0
  • Simulate all 16 input combinations
  • Confirm results match truth table

Explanation

A 2-bit comparator checks the relationship between two binary numbers (A and B) and determines whether A is less than, greater than, or equal to B. Each number is represented using two bits (a1a0 and b1b0), giving 16 total input combinations.

We use three outputs:

  • Z2 = 1 if A < B
  • Z1 = 1 if A > B
  • Z0 = 1 if A = B

The truth table explicitly compares all combinations of A and B from 0 to 3. For each pair, the comparator outputs only one 1 among Z0, Z1, and Z2.

To implement this in logic, we consider bitwise comparisons:

  • For equality, both bits must be equal. This is easily implemented using XNOR gates.
  • For greater than or less than, we compare the most significant bits (a1 vs. b1). If they differ, the result is clear. If they are the same, we then compare the least significant bits (a0 vs. b0).

This logic leads to compact expressions using AND, OR, NOT, and XNOR gates. To minimize gates, shared terms such as (a1 ≡ b1) can be computed once and reused.

In Logisim, these expressions can be directly implemented with basic logic blocks. Testing the circuit against the truth table verifies correctness. This exercise is a fundamental example of digital logic design, showing how to translate a logical requirement into a functional circuit using Boolean algebra and minimization techniques.

Scroll to Top