Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations36
  • Combinational Design25
    • Combinational Timing Overview
    • Muxes, Decoders and Encoders
    • Arithmetic Blocks
    • Comparators and Shifters
      • Magnitude Comparison
      • Barrel Shifter, Rotate and Arithmetic Shift
      • Mini Project: a Leading-Zero Counter and Normaliser
    • Tri-State and Shared Buses
    • Latch Inference
    • Arithmetic Datapaths
  • Sequential Design27
  • FSM & Control27
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹Combinational Design
  1. Topics
  2. RTL Design
  3. Combinational Design
  4. Comparators and Shifters

Part 2 · Combinational · Intermediate

Comparators and Shifters

Comparators order values and shifters move bits for branches, alignment and scaling.

  • 3topics
  • 3lessons

Topics in this section

  1. 1Magnitude Comparison1 lesson
  2. 2Barrel Shifter, Rotate and Arithmetic Shift1 lesson
  3. 3Mini Project: a Leading-Zero Counter and Normaliser1 lesson
On this page

    Comparators decide branches and thresholds, and signedness can flip the answer. Shifters move bits into position with one stage per count bit.

    Lessons in this topic

    3 lessons, about 17 min

    1. 1Magnitude ComparisonYou can build equality and ordering comparators, derive unsigned and signed less-than from the subtractor flags, and avoid the mixed-sign trap.4 min
    2. 2Barrel Shifter, Rotate and Arithmetic ShiftYou can build a log2 N stage barrel shifter, choose the fill for each mode, and state the delay and area cost.4 min
    3. 3Mini Project: a Leading-Zero Counter and NormaliserYou can specify a combinational block with an edge case (the all-zero input), test it by input class plus random volume, and read cell count and logic depth from Yosys.9 min
    Check yourself Which is cheaper in hardware: 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.

    Interview

    Expect “implement a barrel shifter” and “how do you compare two signed numbers using only an adder?”. Both are covered here.

    Revision sheet

    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.

    Practise it

    • 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

    PreviousNext: Magnitude Comparison

    Up next

    Up nextMagnitude Comparison

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Combinational Timing Overview
    • Muxes, Decoders and Encoders
    • Arithmetic Blocks