3.3.7Combinational Circuits

Encoders and priority encoders

2,119 words10 min readdifficulty · medium

WHAT is an encoder?

WHY does this exist? Imagine 8 buttons on a keypad. Running 8 separate wires everywhere is wasteful. Instead, encode "which button" into just 3 wires (log28=3\log_2 8 = 3). Compression of information from position into number.


HOW to derive a 4-to-2 encoder from scratch

We have inputs I0,I1,I2,I3I_0, I_1, I_2, I_3. Only one is HIGH at a time. Outputs Y1Y0Y_1 Y_0 should give the index in binary.

Step 1 — build the truth table. (Why? Because the whole behaviour is just "index → binary".)

Active input Y1Y_1 Y0Y_0
I0I_0 0 0
I1I_1 0 1
I2I_2 1 0
I3I_3 1 1

Step 2 — read off which inputs make each output 1.

  • Y0=1Y_0 = 1 when the active input is I1I_1 or I3I_3 (the odd indices).
  • Y1=1Y_1 = 1 when the active input is I2I_2 or I3I_3 (indices 2\ge 2).

Why this step? Each output bit is just "which indices have a 1 in that bit position?" That's literally the binary representation.

Step 3 — generalize. For a 2n2^n-to-nn encoder: Yk=OR{Ij:bit k of j is 1}Y_k = \sum_{\text{OR}} \{\, I_j : \text{bit } k \text{ of } j \text{ is } 1 \,\}

Why? Output bit kk must be 1 exactly for those input indices jj whose kk-th binary digit is 1.


The PROBLEM with a plain encoder


Priority Encoder — WHAT and WHY

WHY: real systems (interrupts, keypads) can have several requests at once. We need a deterministic rule: "serve the most important one." Priority = a tie-breaker.

Deriving the 4-input priority encoder

Priority: I3>I2>I1>I0I_3 > I_2 > I_1 > I_0. Use ×\times for "don't care".

I3I_3 I2I_2 I1I_1 I0I_0 Y1Y_1 Y0Y_0 VV
0 0 0 0 ×\times ×\times 0
0 0 0 1 0 0 1
0 0 1 ×\times 0 1 1
0 1 ×\times ×\times 1 0 1
1 ×\times ×\times ×\times 1 1 1

Step 1 — VV: valid whenever anything is on. V=I0+I1+I2+I3V = I_0 + I_1 + I_2 + I_3 Why? OR of all inputs = "at least one active."

Step 2 — Y1Y_1: should be 1 for indices 2 and 3. I3I_3 present ⇒ Y1Y_1. Or I2I_2 present and no higher one blocks it — but since Y1=1Y_1=1 for both 2 and 3 anyway: Y1=I2+I3Y_1 = I_2 + I_3 Why the same as before? Both indices 2 and 3 have bit-1 set, and I3I_3 dominates I2I_2 but both give Y1=1Y_1=1, so no suppression needed for this bit.

Step 3 — Y0Y_0: 1 for indices 1 and 3. But I1I_1 must be suppressed if a higher input (I2I_2 or I3I_3) is active (higher ones give Y0=0Y_0=0). Y0=I3+I1I2Y_0 = I_3 + I_1\,\overline{I_2} Why the I2\overline{I_2}? If I2I_2 is active it outranks I1I_1; index 2 wants Y0=0Y_0=0. So I1I_1 only counts when I2I_2 is off. I3I_3 always forces Y0=1Y_0=1 regardless (index 3 is odd).


Worked Examples


Recall Feynman: explain to a 12-year-old

Imagine a row of 8 doorbells, numbered 0 to 7. Instead of running 8 long wires to grandma's room, we send just 3 wires that spell out the number of whichever bell was rung — in binary (like 0-1-1 = bell 3). That gadget is an encoder. But what if two kids ring bells at the same time? A plain encoder gets confused and shouts a wrong number. So we add a rule: "always report the biggest bell number." That polite, rule-following version is a priority encoder. And a tiny extra light (VV) turns on only when someone actually rang, so grandma can tell the difference between "bell 0" and "no one is here."


Flashcards

What is an encoder (in terms of inputs/outputs)?
A 2n2^n-to-nn device: 2n2^n inputs, nn outputs; when one input is active it outputs that input's binary index.
Encoder vs decoder?
Decoder: nn2n2^n (number to one-hot line). Encoder: 2n2^nnn (one-hot line to number). Inverses.
Give the 4-to-2 plain encoder equations.
Y0=I1+I3Y_0 = I_1 + I_3, Y1=I2+I3Y_1 = I_2 + I_3.
Why does a plain encoder fail with multiple active inputs?
OR gates merge bits from different indices, producing a code for neither input (e.g. I1+I211=3I_1{+}I_2 \Rightarrow 11 = 3).
Why does a plain encoder fail with all-zero input?
Output 0000 is ambiguous with "I0I_0 active." Cannot distinguish "nothing" from index 0.
What extra output fixes the all-zero ambiguity?
The Valid bit V=I0+I1+V = I_0+I_1+\dots, HIGH iff at least one input is active.
What does a priority encoder do differently?
When several inputs are active it outputs the code of the highest-priority (highest-index) one, ignoring lower ones.
Give the 4-input priority encoder equations.
Y1=I2+I3Y_1 = I_2+I_3; Y0=I3+I1I2Y_0 = I_3 + I_1\overline{I_2}; V=I0+I1+I2+I3V = I_0+I_1+I_2+I_3.
Why the I2\overline{I_2} term in Y0Y_0?
I2I_2 (index 2) outranks I1I_1; when I2I_2 is active, I1I_1's contribution must be suppressed so index 2's code (10) is produced.
General rule for output bit YkY_k of an encoder?
OR of all inputs IjI_j whose index jj has bit kk equal to 1.

Connections

  • Decoders — the exact inverse operation of an encoder.
  • Multiplexers — muxes select data; encoders select which line is active.
  • Interrupt controllers — real hardware use of priority encoders (which IRQ to serve first).
  • Boolean algebra simplification — used to derive/minimize the OR-based equations.
  • Karnaugh maps — tool to derive priority-encoder logic with don't-cares.
  • Combinational circuits — parent topic; encoders are memoryless logic.

Concept Map

opposite of

behaviour captured by

read off bits

generalizes to

built from

fails when

fails when

fixed by

fixed by

outputs highest index

includes

Decoder

Encoder

Truth table index to binary

4-to-2 equations Y0 Y1

General 2^n to n formula

OR gates only

Multiple inputs HIGH

No input HIGH

Priority encoder

Valid bit V

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, encoder ek aisa circuit hai jo decoder ka ulta kaam karta hai. Decoder chhoti si binary number leke 2n2^n lines me se ek ko HIGH karta hai. Encoder ulta — usme 2n2^n input lines hoti hain, aur jo bhi line active (HIGH) hoti hai, uska index binary me output kar deta hai. Matlab sawaal simple hai: "kaunsi line dabi?" — aur jawaab number ki form me. 8 buttons ko 8 wires ki jagah sirf 3 wires me pack kar dena — yehi iska magic hai (log28=3\log_2 8 = 3).

Har output bit ka rule yaad rakho: YkY_k tab 1 hoga jab active input ka index ke kk-th bit me 1 ho. Isliye 4-to-2 me Y0=I1+I3Y_0 = I_1 + I_3 (odd indices) aur Y1=I2+I3Y_1 = I_2 + I_3 (indices 2\ge 2). Bas OR gates, koi AND nahi — kyunki assume karte hain ek hi input active hai.

Lekin problem tab aati hai jab do inputs ek saath active ho jaayein. Plain encoder confuse ho jaata hai aur galat number de deta hai (jaise I1I_1 aur I2I_2 dono on ⇒ output 3, jo galat hai). Aur agar kuch bhi press na ho, to output 0000 aata hai jo "I0I_0 active" jaisa dikhta hai — dono me farq hi nahi chalega. Iska solution hai priority encoder: jab multiple inputs on hon, sabse bada index jeet jaata hai (Big Boss Wins), aur ek chhota Valid bit VV batata hai ki koi input sach me active hai ya nahi.

Yeh cheez real life me bahut important hai — jaise interrupt controller me, jab kai devices ek saath CPU se attention maangte hain, priority encoder decide karta hai kise pehle serve karna hai. Toh formula ratne se pehle rule samjho: OR se bits banao, aur priority ke liye chhote inputs ko bade input se suppress karo (isliye Y0Y_0 me I2\overline{I_2} aaya).

Go deeper — visual, from zero

Test yourself — Combinational Circuits

Connections