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

  • RTL Foundations36
    • Digital Logic for RTL Engineers
    • Number Systems for RTL Engineers
    • Verilog Fundamentals
    • Verilog Operators and Types
      • Width Discipline
      • Bit Vectors and Slicing
      • Concatenation and Replication
      • Reduction and Logical Operators
      • Shift and Arithmetic Intent
      • Parameterized, Type-Safe Design
    • SystemVerilog for RTL
    • Simulation and Testbenches
  • Combinational Design25
  • Sequential Design27
  • FSM & Control27
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹Verilog Operators and Types
  1. Topics
  2. RTL Design
  3. RTL Foundations
  4. Verilog Operators and Types
  5. Bit Vectors and Slicing

Part 1 · Foundations · Beginner

Bit Vectors and Slicing

How vectors, selects and indexed part-selects work, and what out-of-range reads do.

On this page

    Why this matters

    word[15:8] is plain wiring, but word[8*i +: 8] with a variable i is a four-way byte mux. Reading beyond the vector gives x, and writing there is ignored, so a wrong index fails silently.

    After this lesson you can: select bits, fixed fields and variable byte lanes, and say which selects are wiring and which build a mux.

    Declaring a vector

    logic [7:0] data; is an 8-bit vector with bit 7 as the MSB and bit 0 as the LSB. The [7:0] order matches how numbers are written.

    The reversed order [0:7] is a common source of confusion. Avoid it for data.

    Selecting bits and fields

    Form

    Example

    Meaning

    Bit select

    word[5]

    Bit 5

    Part select (constant)

    word[15:8]

    Bits 15 down to 8 (a byte)

    Indexed part select (up)

    word[8*i +: 8]

    8 bits starting at bit 8·i, going up

    Indexed part select (down)

    word[31 -: 8]

    8 bits ending at bit 31, going down

    The indexed forms let the start position vary while the width stays fixed. That makes them the right tool for extracting byte lanes from a word:

    Byte lanes of 32'hDEADBEEF: lane i covers bits 8i+7 down to 8i, and the two indexed forms count 8 bits up from a start or down from an end

    Read the ruler like the declaration [31:0]: bit 31 on the left, bit 0 on the right, one box per byte lane.

    • `+:` counts up from the start: word[8*1 +: 8] starts at bit 8 and takes bits 8 to 15, the same wires as the fixed word[15:8].

    • `-:` counts down from the end: word[31 -: 8] ends at bit 31 and takes bits 31 to 24, the byte DE.

    • Lane i starts at bit 8·i, so i = 0 is EF, the least significant byte.

    A part-select with variable endpoints (word[hi:lo] where hi and lo are signals) is not allowed because the width must be a constant. Use word[lo +: WIDTH] with a constant width.

    Fixed wiring or a mux?

    word[15:8] always connects the same eight bits: it is fixed wiring, not a runtime choice. If i is a two-bit signal, word[8*i +: 8] must choose one of four bytes, so synthesis builds selection logic equivalent to a four-input byte mux. If i is an elaboration constant, the choice becomes fixed wiring again.

    i

    Selected byte

    For DEADBEEF

    0

    word[7:0]

    EF

    1

    word[15:8]

    BE

    2

    word[23:16]

    AD

    3

    word[31:24]

    DE

    Check yourself Does replacing a fixed byte slice with a signal-selected byte lane add an adder just because the expression contains 8*i?

    Not necessarily: multiplication by eight describes bit-position selection, and synthesis can reduce the whole expression to a mux. The important new hardware is runtime selection. Confirm the mapped cost in the target flow; syntax alone is not a cell count.

    Out-of-range selects

    • Reading a bit index beyond the vector (for example word[40] on a 32-bit vector) gives `x` in simulation, a silent trap.

    • Writing to an out-of-range index is ignored.

    • When the index is a variable, make sure it cannot exceed the range, or guard it explicitly.

    Tip

    Name the fields of a packed word with localparam offsets (for example localparam OPCODE_LSB = 28;) or, in SystemVerilog, with a packed struct. Magic numbers like instr[31:28] scattered through a design are the hardest thing to maintain.

    Check yourself Write the expression that selects byte lane i of a 64-bit bus d, where i is a 3-bit signal.

    d[8*i +: 8]. The start position 8*i varies with i; the width 8 is constant, which the language requires.

    Testbench

    The testbench below checks fixed slices and byte lanes selected by a variable.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    module tb_slicing;
      logic [31:0] word = 32'hDEADBEEF;
      logic [7:0]  b;
      int errors = 0;
    
      initial begin
        if (word[31:28]       !== 4'hD)  errors++;   // top nibble
        if (word[15:8]        !== 8'hBE) errors++;   // fixed byte
        for (int i = 0; i < 4; i++) begin
          b = word[8*i +: 8];                         // byte i, selected by a variable
          if (b !== 8'(word >> (8*i))) errors++;      // same as shifting down and keeping 8 bits
        end
        if (word[8*0 +: 8] !== 8'hEF) errors++;
        if (word[8*1 +: 8] !== 8'hBE) errors++;
        if (word[8*2 +: 8] !== 8'hAD) errors++;
        if (word[8*3 +: 8] !== 8'hDE) errors++;
        if (errors == 0) $display("PASS: slices and indexed part-selects as described");
        $finish;
      end
    endmodule

    Remember

    • Declare [N-1:0] so the MSB is on the left. Avoid ascending ranges for data.

    • A part-select needs constant bounds. Use base +: WIDTH when only the start varies.

    • An out-of-range read gives x and an out-of-range write is ignored.

    Rule of thumb: name the fields of a packed word instead of scattering magic bit numbers.

    Keep going

    • Interview: run a mock interview on this topic

    Related topics

    • Concatenation and replication

    Previous

    20 / 350

    Next: Concatenation and Replication

    Up next

    Up nextConcatenation and Replication

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Reduction and Logical Operators
    • Shift and Arithmetic Intent
    • Parameterized, Type-Safe Design