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

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
    • Datatype and Bit Puzzles
      • X-Bit Comparisons and Mixed Arithmetic
      • Signed vs Unsigned Comparison Trap
      • Type Cast Controls Sign Extension
      • Signed Part-Select Is Still Unsigned
      • X Collapse into 2-State `int`
      • Expression Width Drops the Carry
    • Fork Join Puzzles
    • Queue and Array Puzzles
    • String and Casting Puzzles
    • SystemVerilog Language Puzzles: Tricky Q&A
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←Datatype and Bit Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. SystemVerilog Language Puzzles
  4. Datatype and Bit Puzzles
  5. Signed vs Unsigned Comparison Trap
On this page

    VLSI DV Interview PuzzlesAll levels

    Signed vs Unsigned Comparison Trap

    What are the three outputs, and why does the first comparison surprise people?

    Puzzle

    Difficulty: Medium · Puzzle 2 of 6 · Topic: Datatype and Bit Puzzles

    What are the three outputs, and why does the first comparison surprise people?

    Code

    p2.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    module p2;
      logic signed [3:0] s = -1;
      logic [3:0] u = 4'h1;
      initial begin
        $display("s<u=%0d", s < u);
        $display("$signed(u)>s=%0d", $signed(u) > s);
        $display("s+u=%0d", s + u);
      end
    endmodule

    Hint

    Mixed signed/unsigned expressions follow conversion rules before the operator executes. Also check expression width.

    Step-by-step solution

    Diagram
    1) In `s < u`, `u` is unsigned, so `s` is converted to unsigned 4'b1111 (15); 15 < 1 is false.
    2) `$signed(u)` forces signed compare in second line: 1 > -1 is true.
    3) `s + u` is still mixed signed/unsigned at 4-bit width: 15 + 1 wraps to 0.

    Answer

    Answer: s<u=0, $signed(u)>s=1, s+u=0

    How this was checked

    Checked by running the code in Icarus Verilog 13 and Verilator 5; the printed output supports the answer above. One unsigned operand makes the whole compare unsigned. (Icarus, 4-state) One unsigned operand makes the whole compare unsigned. (Verilator, 2-state)

    Why candidates get it wrong

    `s < u` is false although -1 < 1: one unsigned operand makes the whole comparison unsigned, so -1 becomes 4'b1111 = 15.

    Interviewer follow-up

    If `u` were declared `logic signed [3:0]`, what would `s<u` and `s+u` become? Model answer: both operands are signed, so s<u is 1 (-1 < 1) and s+u is 0 (-1 + 1).

    Say it out loud

    I would first find the signedness of each operand, because one unsigned operand makes the whole comparison unsigned. So minus one becomes 4 bits of ones, which is 15, and 15 less than 1 is false, giving 0. With the signed cast the compare is 1 greater than minus 1, which is true, and the sum wraps to 0 at 4 bits. The trap is believing that the signed variable keeps the expression signed. Next I would check whether every operand in the expression is declared signed.

    Related topics

    • Signed & Unsigned Arithmetic

    • Expression Width & Evaluation Rules

    • Back to Datatype and Bit Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    6 / 293

    Next: Study Route: One Week Before the Interview

    Up next

    Up nextStudy Route: One Week Before the Interview

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • X-Bit Comparisons and Mixed Arithmetic
    • Type Cast Controls Sign Extension
    • Signed Part-Select Is Still Unsigned