RTL Design / Module 3: Verilog from Scratch
Lesson 3.5

Operators, vectors, concatenation & replication

Verilog is not just wires and flops โ€” it is a rich expression language. Master operators, bit vectors, {a,b} concatenation, and {n{x}} replication and you can pack datapaths into a few clean lines.

Core idea

Every expression has a width and a type (signed vs unsigned). Synthesis follows width rules โ€” surprises here become silent bugs in silicon.

1

Operator families at a glance

Verilog groups operators by what they operate on. Confusing & (bitwise) with && (logical) is a classic beginner mistake โ€” they are not interchangeable.

FamilyExamplesOperandsResult width
Arithmetic+ - * / %Vectors (unsigned by default)Max operand width (after extension)
Bitwise& | ^ ~Vectors, bit-by-bitMax operand width
Logical&& || !Any โ€” treated as true/falseAlways 1 bit
Relational== != < <= > >=Vectors comparedAlways 1 bit
Shift<< >> <<< >>>Vector + shift amountLeft operand width
Reduction& | ^ ~& ~| ~^ (unary)Single vectorAlways 1 bit
Concat / replicate{a,b} {4{x}}ExpressionsSum of part widths
Analogy

Bitwise & is like AND-ing each lane of two highways in parallel. Logical && asks "are both highways non-empty?" and returns a single yes/no.

2

Vectors, part-select & indexing

A vector is a bus of bits: wire [7:0] byte; declares 8 bits indexed from 7 (MSB) down to 0 (LSB) โ€” the Verilog convention.

8-bit vector part-select

Indexing and slicing โ€” the bread and butter of datapath RTL.

SyntaxMeaningWidth of result
bus[i]Single bit at index i1
bus[hi:lo]Contiguous slice (inclusive)hi - lo + 1
bus[i+:w]Part-select from i, width w, ascendingw
bus[i-:w]Part-select from i, width w, descendingw
part_select.v
wire [15:0] word;
wire [7:0]  upper_byte = word[15:8];
wire [3:0]  nibble_hi  = word[15:12];
wire        parity_bit = ^word;           // reduction XOR
wire [3:0]  lane       = word[8+:4];      // bits [11:8]
assign word[7:0] = 8'hA5;                 // drive low byte only (upper undriven here โ€” illustrative)
3

Arithmetic & width rules

When operands differ in width, Verilog zero-extends the shorter one (unsigned context). Addition result width is the max of operand widths โ€” not automatically one bit wider for carry!

Overflow trap: 8'hFF + 8'h01 in 8-bit context wraps to 8'h00. If you need carry, widen explicitly: {1'b0,a} + {1'b0,b}.

width_rules.v
wire [3:0] a = 4'd10;
wire [3:0] b = 4'd7;
wire [3:0] sum_narrow = a + b;        // 4-bit result โ€” may wrap
wire [4:0] sum_safe   = {1'b0,a} + {1'b0,b}; // 5-bit โ€” room for carry

// Signed arithmetic โ€” declare signed or cast
wire signed [7:0] x = -8'sd3;
wire signed [7:0] y =  8'sd5;
wire signed [7:0] diff = x - y;       // signed context
Trap

Mixing signed and unsigned silently promotes to unsigned in many tools. Mark ports signed or use $signed() when doing two's-complement math.

4

Bitwise, logical & relational

Expressiona=4'b1010, b=4'b0011Notes
a & b4'b0010Bitwise AND per bit
a && b1'b1Both non-zero โ†’ true
a | b4'b1011Bitwise OR
a == b1'b0Vector equality โ†’ 1 bit
a === b4-state compareSimulation only โ€” avoid in RTL
mask_enable.v
// Common pattern: mask then OR into status register
wire [7:0] status;
wire [7:0] mask   = 8'b0000_1111;
wire [7:0] new_lo = data_in & mask;
wire       any_err = |error_flags;     // reduction OR โ†’ 1 if any bit set
wire       all_ok  = &ready_flags;     // reduction AND โ†’ 1 if all bits set
5

Shift operators

<< and >> are logical shifts (zero-fill). <<< and >>> are arithmetic when the left operand is signed (sign-extend on right shift).

OperatorNameFill bitsTypical use
<<Logical leftโ€”Multiply by 2n
>>Logical right0Unsigned divide by 2n
>>>Arithmetic rightMSB (sign)Signed divide by 2n
shifts.v
wire [7:0] cnt = 8'd5;
wire [7:0] doubled = cnt << 1;           // 10
wire signed [7:0] neg = -8'sd8;          // 8'b1111_1000
wire signed [7:0] arith = neg >>> 1;   // -4, sign preserved
6

Concatenation {a,b} & replication {n{x}}

Curly braces build wider vectors. Order is MSB on the left: {a,b} puts a in the upper bits.

Concatenation diagram

{4{byte}} replicates byte four times โ†’ 32-bit bus.

concat_replicate.v
wire [7:0]  tag  = 8'hA5;
wire [31:0] word = {tag, 24'h00_00_01}; // tag in MSBs
wire [31:0] pat  = {8{4'hF}};           // 32'hFFFF_FFFF
wire [7:0]  zero = {8{1'b0}};         // same as 8'b0
wire [15:0] padded = {{8{1'b0}}, byte_in}; // zero-extend 8โ†’16
Pro tip

{WIDTH{1'b0}} is the idiomatic way to write an all-zero constant of arbitrary parameter width โ€” you'll see it in every counter reset.

7

Reduction operators

Unary reduction ops fold an entire vector to a single bit โ€” perfect for any/all style flags.

OpMeaningExample: x=4'b1010
&xAND all bits0
|xOR all bits1
^xXOR all bits (parity)1
~&xNAND reduction1
8

Worked example โ€” packet header builder

Combine everything: slice fields, concat a header, use reduction for parity.

header_builder.v
module header_builder (
  input  wire [7:0]  src_id,
  input  wire [7:0]  dst_id,
  input  wire [15:0] payload_len,
  output wire [31:0] header,
  output wire        len_ok
);
  wire [3:0] version = 4'h1;
  wire [3:0] flags   = 4'h0;

  assign header = {version, flags, src_id, dst_id, payload_len};
  assign len_ok   = |payload_len;  // non-zero length check
endmodule
โ–ถ Try operators in the Verilog simulator
  • Build {a,b} and predict the bit width before sim
  • Compare & vs && on the same inputs
  • Write an 8-bit increment that widens to 9 bits for carry-out

๐ŸŽฏ What to remember

  • Know operator families โ€” bitwise โ‰  logical โ‰  reduction.
  • Width rules drive overflow; widen explicitly when you need carry.
  • {a,b} concatenates MSB-left; {n{x}} replicates.
  • Part-select and indexing are how you move data through a datapath.
  • Always ask: "What width is this expression?" before you synthesize.
Advertisement