Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Modules and Data Types

Verilog Vectors, Bit-Selects and Concatenation

Learn Verilog vectors: declaring buses, selecting bits and slices, indexed part-selects, concatenation and replication, endianness and arrays, with runnable C bit-manipulation checks.

Digital hardware is full of buses: 8-bit bytes, 32-bit words, 5-bit register numbers. In Verilog a bus is a vector: a wire or reg with a width. This lesson shows how to declare vectors, pick out individual bits or ranges, glue vectors together and replicate patterns.

Declaring vectors

VERILOG
wire [7:0]  byte_bus;     // 8 bits, bit 7 is the most significant
reg  [31:0] word;
wire [0:3]  nibble;       // bit 0 is the MOST significant (rare, avoid)
wire        flag;         // a scalar (1 bit)

The range [msb:lsb] gives the index of the most significant and least significant bit. The convention [N-1:0] (MSB on the left, LSB is bit 0) is standard; use it everywhere. The width is msb - lsb + 1.

Bit-select and part-select

VERILOG
wire [7:0] d = 8'b1011_0010;

wire b3   = d[3];       // one bit  -> 0
wire [3:0] hi = d[7:4]; // slice    -> 4'b1011
wire [3:0] lo = d[3:0]; // slice    -> 4'b0010

The bounds of a normal part-select must be constants. For a slice whose position changes at run time, use an indexed part-select:

VERILOG
// starts at 'i', goes UP 4 bits: bits [i+3:i]
wire [3:0] up   = d[i +: 4];
// starts at 'i', goes DOWN 4 bits: bits [i:i-3]
wire [3:0] down = d[i -: 4];

The width after +: or -: is the constant; the start can be a variable. This is how you extract a byte from a wide word: word[8*k +: 8].

A variable single-bit select d[i] is also legal and builds a multiplexer in hardware. If the index is out of range, the result is x in simulation.

Concatenation

Braces join vectors into a wider one; the leftmost item becomes the most significant bits.

VERILOG
wire [3:0] a = 4'hA, b = 4'h5;
wire [7:0] c = {a, b};            // 8'hA5
wire [8:0] with_carry = {1'b0, c};// zero-extend by one bit
{carry, sum} = x + y;             // split a wide result into two targets

Concatenation works on the left side of an assignment too, which is perfect for adders: assign {cout, sum} = a + b + cin;.

Replication

{N{value}} repeats value N times (N must be a constant):

VERILOG
wire [7:0] ones   = {8{1'b1}};          // 8'hFF
wire [15:0] pat   = {4{4'b1010}};       // 16'hAAAA
wire [31:0] sext  = {{24{b[7]}}, b};    // sign-extend an 8-bit value

Sign extension replicates the sign bit: copy b[7] 24 times in front of b. SystemVerilog and the signed keyword can do this automatically, but knowing the explicit form helps you read real code.

Common vector idioms

Goal Verilog
Swap halves of a byte {d[3:0], d[7:4]}
Reverse bit order (4 bits) {d[0], d[1], d[2], d[3]}
Shift left by 1, shift in x {d[6:0], x}
Rotate left by 1 {d[6:0], d[7]}
Any bit set? |d (reduction OR)
All bits set? &d (reduction AND)
Even parity ^d (reduction XOR)
Low-bit test d[0]

The reduction operators in the last rows are covered in the operators lesson.

Arrays of vectors (memories)

A vector holds one bus. To hold many, declare an array:

VERILOG
reg [7:0] mem [0:15];            // 16 bytes
wire [7:0] first = mem[0];       // word select
wire bit5 = mem[3][5];           // bit 5 of word 3 (Verilog-2001)

You cannot assign a whole array in one statement in plain Verilog; loop over the words. SystemVerilog adds packed and unpacked arrays and bulk operations.

Endianness in practice

Verilog does not impose byte order; the order you concatenate in is the order you get. When connecting to a memory or bus protocol, document whether byte 0 is the least or most significant. A bug here usually appears as "the data is right but the bytes are swapped".

Stand-in: slices and concatenation in C

This stand-in (not Verilog) implements get_bits, put_bits, concatenation and replication on 32-bit integers, then performs the idioms in the table so you can check them.

C Stand-in: part-select, concatenation, replication (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Sign extension deserves its own check. This stand-in applies {{24{b[7]}}, b} to four byte values:

C Stand-in: sign extension with replication (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is {4'hA, 4'h5}?

    The leftmost item in a concatenation becomes the most significant bits.
  2. 2. What does d[i +: 4] select?

    +: starts at the variable index and counts upward by the constant width.
  3. 3. What does {8{1'b1}} produce?

    Replication repeats 1'b1 eight times.
  4. 4. How do you sign-extend an 8-bit b to 32 bits by hand?

    Replicate the sign bit b[7] 24 times above b.

Summary

  • Declare vectors as [msb:lsb], preferring [N-1:0].
  • Select one bit with d[i], a constant slice with d[7:4], and a variable slice with d[i +: w].
  • Concatenation {a, b} joins vectors (leftmost is most significant); replication {N{x}} repeats a pattern.
  • Sign extension replicates the sign bit; memories are arrays declared as reg [w-1:0] mem [0:depth-1].

Next: Verilog Operators