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

  • Start Here6
  • SystemVerilog Language Foundations28
  • Object-Oriented SystemVerilog for Verification30
  • Constraint Randomization — Interview-Critical Deep Dive36
    • Randomization Basics: rand, randc & randomize()
    • Constraint Operators & Expressions
    • Dynamic Constraint Control
    • Randomizing Arrays and Queues
    • How the Constraint Solver Works
    • Debugging Randomization Failures
    • Interview Drills — Constraints and Randomization Q&A
      • Even/Odd, Alignment & Divisibility
      • Powers of 2, One-Hot & Bit Games
      • Unique Values & Ordering
      • Sum, Checksum & Distribution Drills
      • Dependent-Field Puzzles
      • Q&A: rand, randc & randomize()
      • Q&A: dist, inside, soft, solve-before
      • Q&A: Controlling Randomization
      • Q&A: Arrays & Nested Objects
  • SystemVerilog Assertions — Interview-Critical Deep Dive40
  • Functional Coverage in SystemVerilog22
  • Testbench Architecture in SystemVerilog23
  • Advanced Language & Integration25
  • Senior & Interview Preparation21
  • Principal-Level Verification28
Loading lesson

‹Interview Drills — Constraints and Randomization Q&A
  1. Topics
  2. SystemVerilog
  3. Constraint Randomization — Interview-Critical Deep Dive
  4. Interview Drills — Constraints and Randomization Q&A
  5. Powers of 2, One-Hot & Bit Games

Part 3 · Constraint Randomization · Intermediate

Powers of 2, One-Hot & Bit Games

Drill: a power of two, an exact count of set bits, and a gray-code neighbour.

On this page

    Why this matters

    A grant-mask drill passed every random check and still failed review: (x & (x - 1)) == 0 let the all-zero mask through.

    After this lesson you can: write powers of two, exact set-bit counts and gray neighbours with the bit functions interviewers expect.

    The three prompts

    Drill 1. rand bit [15:0] x: x is a power of two.

    Drill 2. rand bit [7:0] m: exactly three bits of m are set.

    Drill 3. rand bit [7:0] a, b: b differs from a in exactly one bit.

    Write the three answers before opening the reveal. Copy the starter into the Code Lab, fill in each empty answer_c, and run it on Verilator. The grader solves each class hundreds of times and checks every result, then pins a few values that must be legal; it prints ok or the first rule your answer broke.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // Drill starter: write each answer_c, then run it.
    // Drills: powers of two, set-bit counts and gray neighbours.
    class drill1;                                   // x is a power of two
      rand bit [15:0] x;
      constraint answer_c { /* your answer here */ }
    endclass
    
    class drill2;                                   // exactly 3 of the 8 bits of m are set
      rand bit [7:0] m;
      constraint answer_c { /* your answer here */ }
    endclass
    
    class drill3;                                   // b differs from a in exactly one bit
      rand bit [7:0] a, b;
      constraint answer_c { /* your answer here */ }
    endclass
    
    Predict first Before you write drill 1: does (x & (x - 1)) == 0 alone give a power of two?

    No. Zero satisfies it, and zero is not a power of two. Add x != 0, or use $countones(x) == 1.

    Check yourself Reveal one set of answers (open only after your own run)

    One set of answers, run through the same grader:

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // Drills: powers of two, set-bit counts and gray neighbours.
    class drill1;                                   // x is a power of two
      rand bit [15:0] x;
      constraint answer_c { x != 0; (x & (x - 1)) == 0; }
    endclass
    
    class drill2;                                   // exactly 3 of the 8 bits of m are set
      rand bit [7:0] m;
      constraint answer_c { $countones(m) == 3; }
    endclass
    
    class drill3;                                   // b differs from a in exactly one bit
      rand bit [7:0] a, b;
      constraint answer_c { $countones(a ^ b) == 1; }
    endclass
    
    Terminal
    drill 1, power of two: ok
    drill 2, three bits set: ok
    drill 3, gray neighbour: ok
    PASS: all 3 drills right

    Why these answers

    Function

    True when

    Whiteboard use

    $onehot(x)

    Exactly one bit is 1

    Powers of two, single grants

    $onehot0(x)

    At most one bit is 1

    Grants where idle is legal

    $countones(x)

    Always: returns the count

    Bands, gray distance, fairness

    A power of two has exactly one bit set, so $countones(x) == 1 and the bit trick with x != 0 are the same set of 16 values. Exact counts and bands are one system function: $countones(m) == 3, or inside {[3:5]} for a band. Candidates who hand-roll a foreach sum over bits signal that they missed it.

    XOR turns differing bits into ones, so a gray neighbour is a one-hot XOR: $countones(a ^ b) == 1. It composes two primitives, which interviewers reward.

    A note on the grader: Verilator 5.048 spread drill 3 very unevenly. In 300 calls the flipped bit was bit 6 in 272 of them and bits 0, 2 and 4 never, though all eight are equally legal. The pinned values catch an answer that is too tight; only coverage would catch a solver that is.

    Common pitfall

    An unsized 1 << k is fine inside a wide comparison, because the 1 widens to the context first. It truncates at 32 bits in self-determined places such as a concatenation or an int temporary, so sizing the literal is a cheap habit.

    Interview

    Likely follow-ups: bound the power into a range, allow idle grants with $onehot0, and cap a Hamming distance with $countones of the XOR. Each reuses one line from this page.

    Remember

    • $countones covers one-hot, bands and Hamming distance.

    • (x & (x - 1)) == 0 admits 0; add x != 0.

    • A gray neighbour is a one-hot XOR.

    Rule of thumb: test the zero and all-ones corners of every bit trick.

    How this was checked

    Run with Verilator 5.048 (--binary --timing) on 2026-10-18; every bench printed the PASS line shown. Verilator prints a harmless WIDTHTRUNC warning for if (!obj.randomize()), because randomize() returns an int, and an UNSATCONSTR warning when a solve fails on purpose. Icarus 13 does not support constraints.

    Each starter was run with its answers left empty, and its grader reported all three drills wrong; each revealed answer passed the same grader. The graders also caught an unwidened size * count and an over-tight even rule when tried.

    Verilator flipped bit 6 in 272 of 300 gray-neighbour solves and bits 0, 2 and 4 never, though all are legal.

    Related topics

    • Unique Values and Ordering

    Previous

    93 / 259

    Next: Unique Values & Ordering

    Up next

    Up nextUnique Values & Ordering

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Sum, Checksum & Distribution Drills
    • Dependent-Field Puzzles
    • Q&A: rand, randc & randomize()