Comparators decide branches and thresholds, and signedness can flip the answer. Shifters move bits into position with one stage per count bit.
3 lessons, about 17 min
a == b or a < b on N bits?Equality: N XNOR gates plus an AND tree, with depth ≈ log₂ N. Magnitude: a subtract-like carry chain (or its optimized equivalent). Equality is often simpler in this model, so prefer it when it is enough; actual cost depends on architecture, library, constraints and mapping.
The whole topic on one page, taken from each lesson's Remember box. Read it before an interview, or print it.
Magnitude Comparison
Equality is an XNOR per bit and an AND tree. Ordering is a subtraction in disguise.
Unsigned a < b is ~C (the borrow). Signed a < b is N ^ V.
If either operand is unsigned, the whole comparison is unsigned. Declare both signed.
Rule of thumb: declare both operands signed, or the comparison is unsigned.
Barrel Shifter, Rotate and Arithmetic Shift
Stage k shifts by 2^k when amt[k] is set, so log2 N stages cover every shift from 0 to N-1.
Fill decides the mode: 0 for logical, the sign bit for arithmetic right, wraparound for rotate.
Delay is log2 N mux stages; area is about N * log2 N muxes. >>> is arithmetic only on a signed operand.
Mini Project: a Leading-Zero Counter and Normaliser
A normaliser finds the shift as it goes: each stage tests the top 2^i bits and its yes or no is bit i of the leading-zero count.
The stages must run largest first, like any binary search; a barrel shifter's order only works because its amount is known.
Random inputs almost never have many leading zeros, so test every first-one position as its own class.
Rule of thumb: if the count is discovered rather than given, decide its most significant bit first.
Code lab: Build a signed comparator
Practice question: A barrel shifter with left, right and rotate modes
Code lab: Build a barrel shift left
Practice question: Priority encoder with parameterized width and valid output
Interview: run a mock interview on this topic