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. Type Cast Controls Sign Extension
On this page

    VLSI DV Interview PuzzlesAll levels

    Type Cast Controls Sign Extension

    Predict `a`, `b`, and `c`. Why are `b` and `c` different from `a`?

    Puzzle

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

    Predict `a`, `b`, and `c`. Why are `b` and `c` different from `a`?

    Code

    p3.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    module p3;
      logic [7:0] u8 = 8'h80;
      logic signed [15:0] a, b, c;
      initial begin
        a = u8;
        b = signed'(u8);
        c = $signed(u8);
        $display("a=%0d b=%0d c=%0d", a, b, c);
      end
    endmodule

    Hint

    Assignment from unsigned extends with zeros. `signed'(...)` and `$signed(...)` reinterpret source sign first, then assignment extends.

    Step-by-step solution

    Diagram
    1) `a = u8` zero-extends 8'h80 into 16'h0080, which is +128.
    2) `signed'(u8)` treats the 8-bit pattern as signed before assignment, so 8'h80 is -128 and sign-extends to 16'hFF80.
    3) `$signed(u8)` has the same signed reinterpretation effect here, so `c` is also -128.

    Answer

    Answer: a=128, b=-128, c=-128

    How this was checked

    Checked by running the code in Icarus Verilog 13 and Verilator 5; the printed output supports the answer above. The signedness of the right-hand side decides sign extension. (Icarus, 4-state) The signedness of the right-hand side decides sign extension. (Verilator, 2-state)

    Why candidates get it wrong

    The signedness of the destination does not decide the extension: the signedness of the right-hand expression does, so `a = u8` zero-extends even though `a` is signed.

    Interviewer follow-up

    What value would `b` have if `u8` were `8'h7F` instead? Model answer: 127, because the sign bit is 0 and sign extension adds zeros, identical to zero extension.

    Say it out loud

    I would ask what the right-hand expression is, because its signedness, not the destination, decides the extension. Plain assignment of the unsigned byte zero-extends, so I get 128, while the signed cast and the dollar signed both reinterpret the pattern as minus 128 and sign-extend. The trap is that the destination being signed does not help, since the extension is decided before the assignment. I would then check the case where the top bit is zero, where both forms agree.

    Related topics

    • Integer Types & Casting

    • Signed & Unsigned Arithmetic

    • Back to Datatype and Bit Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    8 / 293

    Next: Signed Part-Select Is Still Unsigned

    Up next

    Up nextSigned Part-Select Is Still Unsigned

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • X-Bit Comparisons and Mixed Arithmetic
    • Signed vs Unsigned Comparison Trap
    • X Collapse into 2-State `int`