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

  • 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`
      • Case Equality vs Wildcard Equality
    • 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
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • Interview Prep: Puzzle Rounds5
  • 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

VLSI DV Interview Puzzles · All levels

Signed vs Unsigned Comparison Trap

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

On this page

    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

    Why candidates get it wrong

    People reason from mathematical values (-1 and 1) without first applying SV type-conversion rules.

    Interviewer follow-up

    If `u` were declared `logic signed [3:0]`, what would `s<u` and `s+u` become?

    Related topics

    • Back to Datatype and Bit Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    6 / 210

    Next: Type Cast Controls Sign Extension

    Up next

    Up nextType Cast Controls Sign Extension

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • X-Bit Comparisons and Mixed Arithmetic
    • Signed Part-Select Is Still Unsigned
    • X Collapse into 2-State `int`