3.3.9Combinational Circuits

Comparators

1,843 words8 min readdifficulty · medium

1. What are we actually building?

WHAT is the simplest one? A 1-bit comparator. Compare a single bit AA against a single bit BB.

HOW do we start? Write the truth table — 2 inputs → 4 rows.

AA BB A>BA>B A=BA=B A<BA<B
0 0 0 1 0
0 1 0 0 1
1 0 1 0 0
1 1 0 1 0

2. Deriving the 1-bit logic from scratch

WHY these equations? Read the truth table column by column and translate each "1" into a product term.

A>BA>B: the only row where this is 1 is A=1,B=0A=1, B=0. That is the product term ABˉA\bar B. G=ABˉG = A\bar B

A<BA<B: the only 1 is at A=0,B=1A=0, B=1, i.e. AˉB\bar A B. L=AˉBL = \bar A B

A=BA=B: 1 when both are 0 or both are 1. That is exactly the XNOR: E=AˉBˉ+AB=ABE = \bar A\bar B + AB = \overline{A\oplus B}


3. Building a multi-bit comparator (the real trick)

WHY not just a giant truth table? For nn-bit numbers you'd need 22n2^{2n} rows — for 4-bit that's 256 rows. Impossible to hand-design. We need a structured, first-principles method: think like a human comparing numbers.

HOW to formalise for bits. Let A=An1A0A = A_{n-1}\dots A_0, B=Bn1B0B=B_{n-1}\dots B_0.

Define per-bit equality xi=AiBix_i = \overline{A_i\oplus B_i} (1 when bit ii matches).

A>BA>B happens if, scanning from the top, the highest differing bit has Ai=1,Bi=0A_i=1, B_i=0, and all higher bits were equal:

G=An1Bˉn1+xn1An2Bˉn2+xn1xn2An3Bˉn3+G = A_{n-1}\bar B_{n-1} + x_{n-1}A_{n-2}\bar B_{n-2} + x_{n-1}x_{n-2}A_{n-3}\bar B_{n-3} + \dots

WHY the xx chain? The term xn1xn2x_{n-1}x_{n-2} means "all bits above position n3n-3 were equal, so this is the first place they differ" — exactly the human rule.

Symmetrically:

L=Aˉn1Bn1+xn1Aˉn2Bn2+L = \bar A_{n-1}B_{n-1} + x_{n-1}\bar A_{n-2}B_{n-2} + \dots

And equality is "every bit matches": E=xn1xn2x1x0E = x_{n-1}\,x_{n-2}\cdots x_1\,x_0

Figure — Comparators

4. Worked example — 2-bit comparator from scratch

Compare A=A1A0A=A_1A_0 with B=B1B0B=B_1B_0.

Step 1 — per-bit equals. x1=A1B1x_1=\overline{A_1\oplus B_1}, x0=A0B0x_0=\overline{A_0\oplus B_0}. Why this step? These are the reusable "match" signals the human method needs.

Step 2 — Equal. Both bits match: E=x1x0E = x_1 x_0 Why? Numbers are equal only if MSB and LSB agree.

Step 3 — Greater. MSB decides first; if MSBs tie, LSB decides: G=A1Bˉ1+x1A0Bˉ0G = A_1\bar B_1 + x_1\,A_0\bar B_0 Why? First term: MSB of AA wins outright. Second: MSBs equal (x1x_1), so LSB A0Bˉ0A_0\bar B_0 breaks the tie.

Step 4 — Less. Mirror image: L=Aˉ1B1+x1Aˉ0B0L = \bar A_1 B_1 + x_1\,\bar A_0 B_0

Check with A=102=2A=10_2=2, B=012=1B=01_2=1: A1Bˉ1=11=1A_1\bar B_1 = 1\cdot 1 =1G=1G=1. ✔ (2 > 1)

Check with A=01A=01, B=01B=01: x1=1,x0=1x_1=1,x_0=1E=1E=1. ✔


5. Worked example — the IC 7485 (4-bit) cascade

WHY care? Real chips like the 7485 take cascade inputs (A>B)in,(A=B)in,(A<B)in(A>B)_{in},(A=B)_{in},(A<B)_{in} so you can chain them for wider numbers.

Rule when tie occurs internally: if the 4 local bits are all equal, the chip passes through the cascade input.

(A>B)out=Glocal+Elocal(A>B)in(A>B)_{out} = G_{local} + E_{local}\cdot(A>B)_{in}

Why this step? ElocalE_{local} means "these 4 bits didn't decide anything, so trust the less-significant chip below" — again the human first-differing-digit rule, now across whole chips.

Cascade wiring for 8-bit: feed the low nibble's outputs into the high nibble's cascade inputs. The low chip's ground-level inputs are tied (A=B)in=1(A=B)_{in}=1 (so if everything equals, output equals).


6. Common mistakes


7. Active recall

Recall Test yourself (hide answers)
  • Which gate implements 1-bit equality? → XNOR
  • Which bit decides magnitude first? → the MSB
  • Why guard lower-bit terms with xix_i? → so they only act when higher bits tied
  • What does ElocalE_{local} do in a cascaded 7485? → passes cascade input through
  • Sum identity that must always hold? → G+E+L=1G+E+L=1
Recall Feynman: explain to a 12-year-old

Imagine two kids showing number cards, biggest first. They flip the front card: if one is bigger, that kid wins the whole game — no need to look further. If the front cards match, they move to the next card, and so on. If every card matches, it's a tie. A comparator is a tiny electric machine that plays this exact card game with 1s and 0s, instantly.


8. Flashcards

What three outputs does a comparator produce?
A>BA>B (G), A=BA=B (E), A<BA<B (L), mutually exclusive.
1-bit equality equation?
E=ABE=\overline{A\oplus B} (XNOR).
1-bit greater-than equation?
G=ABˉG=A\bar B.
1-bit less-than equation?
L=AˉBL=\bar A B.
Why compare MSB first?
Magnitude is dominated by the most significant bit; the first differing bit decides.
Purpose of the xi=AiBix_i=\overline{A_i\oplus B_i} signals?
Per-bit equality flags used to guard lower-bit decision terms.
2-bit greater-than expression?
G=A1Bˉ1+x1A0Bˉ0G=A_1\bar B_1 + x_1 A_0\bar B_0.
n-bit equality expression?
E=xn1xn2x0E=x_{n-1}x_{n-2}\cdots x_0 (all bits match).
What identity always holds among comparator outputs?
G+E+L=1G+E+L=1.
Role of cascade inputs in the 7485 IC?
Chain multiple comparators; used when local bits are all equal to break the decision at less-significant stage.
7485 cascaded output formula for A>B?
(A>B)out=Glocal+Elocal(A>B)in(A>B)_{out}=G_{local}+E_{local}(A>B)_{in}.
Common XOR-vs-XNOR trap?
XOR detects difference (inequality); equality needs XNOR.

9. Connections

  • XOR and XNOR Gates — the equality primitive
  • Truth Tables and Minterms — how we read out G,E,LG,E,L
  • Binary Subtraction — subtracting and checking the sign bit is an alternate comparison method
  • Multiplexers — another decision-making combinational block
  • Ripple Carry Adder — contrast: adders go LSB-first, comparators go MSB-first
  • 7485 IC — the standard cascadable comparator chip

Concept Map

motivates

outputs

constraint

simplest case

from

derive

derive

derive

equality is

scales to

mimics

needs

avoids

Computers ask is bigger

Comparator

G, E, L signals

G+E+L=1

1-bit comparator

Truth table 4 rows

G = A NOT B

L = NOT A B

E = XNOR A B

XNOR = not differ

Multi-bit comparator

Scan MSB down

Equality chain x_i

2^2n truth table

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Comparator ka kaam simple hai: do binary numbers AA aur BB ko compare karo aur batao kaun bada hai — teen output milte hain: A>BA>B, A=BA=B, aur A<BA<B. Ek time pe sirf ek hi output high hota hai, isliye G+E+L=1G+E+L=1 hamesha true rahta hai. Sabse chhota comparator 1-bit ka hota hai: G=ABˉG=A\bar B, L=AˉBL=\bar A B, aur equality ke liye XNOR use hota hai kyunki XNOR tabhi 1 deta hai jab dono bits same ho.

Sabse important intuition: hum numbers ko MSB se compare karte hain, LSB se nahi. Jaise aap 3746 aur 3752 compare karte ho — aage se digit dekhte ho, jahan pehli baar difference milta hai wahi decide kar deta hai, baaki digits matter nahi karte. Hardware bhi bilkul yehi karta hai. Isliye har lower-bit term ko xi=AiBix_i=\overline{A_i\oplus B_i} (matching flag) se "guard" karte hain — matlab lower bit tabhi bolega jab upar ke saare bits equal ho.

Common galti: log equality ke liye XOR laga dete hain, par XOR to "difference" batata hai, ulta jawab. Equality ke liye hamesha XNOR. Doosri galti: LSB se compare karna — galat, kyunki 1000>01111000 > 0111 hota hai even though LSBs mein BB bade dikhte hain.

Real chips jaise 7485 mein cascade inputs hote hain taaki chhote comparators ko jodkar bade numbers compare kar sako. Rule: agar local 4 bits sab equal ho (Elocal=1E_{local}=1), to niche wale (less-significant) chip ka jawab pass kar do — same MSB-first logic, bas ab poore chips ke level pe.

Go deeper — visual, from zero

Test yourself — Combinational Circuits

Connections