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
    • Fork Join Puzzles
    • Queue and Array Puzzles
    • String and Casting Puzzles
      • String Substr with Runtime Class Cast
      • putc/getc Index Semantics
      • Case-Sensitive vs Case-Insensitive Compare
      • Enum Safety with $cast
      • Failed Class Cast Leaves Old Handle
      • String Number Parsing Stops Early
    • 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

‹String and Casting Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. SystemVerilog Language Puzzles
  4. String and Casting Puzzles
  5. Enum Safety with $cast

VLSI DV Interview Puzzles · All levels

Enum Safety with $cast

Predict cast success bits and enum value after each attempt.

On this page

    Puzzle

    Difficulty: Medium · Puzzle 4 of 6 · Topic: String and Casting Puzzles

    Predict cast success bits and enum value after each attempt.

    Code

    p4.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    typedef enum int {IDLE=0, BUSY=1, DONE=2} state_e;
    
    module p4;
      int raw1 = 2;
      int raw2 = 3;
      state_e s = IDLE;
      initial begin
        $display("c1=%0d s=%s", $cast(s, raw1), s.name());
        $display("c2=%0d s=%s", $cast(s, raw2), s.name());
      end
    endmodule

    Hint

    `$cast` to enum succeeds only for legal enumerator values. On failure, destination keeps previous value.

    Step-by-step solution

    Diagram
    1) `raw1=2` is legal enum value DONE, so first cast returns 1 and `s` becomes DONE.
    2) `raw2=3` is not a legal enumerator, so second cast returns 0.
    3) Failed cast does not modify destination, so `s` stays DONE.

    Answer

    Answer: First line: c1=1 s=DONE; second line: c2=0 s=DONE

    Why candidates get it wrong

    People assume failed `$cast` zeroes destination or sets it to first enum literal.

    Interviewer follow-up

    How would a static cast `state_e'(raw2)` behave differently?

    Related topics

    • Back to String and Casting Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    26 / 210

    Next: Failed Class Cast Leaves Old Handle

    Up next

    Up nextFailed Class Cast Leaves Old Handle

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • String Substr with Runtime Class Cast
    • putc/getc Index Semantics
    • String Number Parsing Stops Early