Module 4: Introduction to Verilog HDL · 4.3 Operators

Shift and Concatenation Operators

Module 2 implemented barrel shifters using cascaded multiplexer stages. In Verilog, the shift operator << instructs the synthesis tool to infer these multiplexer stages automatically. This article examines the four Verilog shift operators, constant versus variable shift hardware costs, and vector concatenation techniques.

Shift Operator Definitions

Verilog provides four distinct shift operators:

Operator Functional Description Vacated Bit Fill Value
<< Logical shift left Zero (0)
>> Logical shift right Zero (0)
<<< Arithmetic shift left Zero (0); identical to <<
>>> Arithmetic shift right Sign bit (MSB) if signed; zero if unsigned
wire [7:0] data = 8'b1010_1100;    // Unsigned decimal 172

wire [7:0] lsl1 = data << 1;       // Evaluates to 8'b0101_1000 (multiplication by 2; MSB discarded)
wire [7:0] lsl3 = data << 3;       // Evaluates to 8'b0110_0000 (multiplication by 8)
wire [7:0] lsr1 = data >> 1;       // Evaluates to 8'b0101_0110 (unsigned division by 2; decimal 86)
wire [7:0] lsr3 = data >> 3;       // Evaluates to 8'b0001_0101 (decimal 21)

A logical left shift by $k$ bits multiplies an unsigned number by $2^k$ if high bits do not overflow. A logical right shift by $k$ bits performs integer division by $2^k$.

Signed Arithmetic Right Shifts

When shifting signed two’s-complement numbers, a right shift must preserve the sign bit. The arithmetic right shift operator >>> replicates the sign bit only when the operand is declared as signed:

wire signed [7:0] sdata = 8'b1010_1100;    // Signed decimal -84

wire signed [7:0] asr1 = sdata >>> 1;    // Evaluates to 8'b1101_0110 (decimal -42)
wire signed [7:0] asr2 = sdata >>> 2;    // Evaluates to 8'b1110_1011 (decimal -21)
wire signed [7:0] bad  = sdata >> 1;     // Evaluates to 8'b0101_0110 (decimal +86: sign lost)
wire signed [7:0] asl1 = sdata <<< 1;    // Identical to logical left shift (<< 1)

If you apply >>> to an unsigned signal, Verilog treats the operand as unsigned and inserts zeros. Declare the target variable as signed when implementing arithmetic right shifts.

Synthesized Hardware for Shifts

When the shift amount is an elaboration-time constant, the shift requires zero logic gates. The synthesis tool reorders physical wire connections directly:

wire [7:0] a;
wire [7:0] b = a << 2;    // Synthesizes as direct wiring: {a[5:0], 2'b00}

When the shift amount is a variable signal, the synthesis tool infers a barrel shifter:

wire [7:0] data;
wire [2:0] amt;                    // Variable shift amount: 0 to 7
wire [7:0] shifted = data << amt;

An 8-bit barrel shifter controlled by a 3-bit variable infers three cascaded stages of 2-to-1 multiplexers. Stage 0 shifts by 1 bit, stage 1 shifts by 2 bits, and stage 2 shifts by 4 bits. The total circuit delay equals three multiplexer delays.

Vector Concatenation

The concatenation operator {} joins multiple vectors and literals into a single vector. The leftmost item forms the most significant bits:

wire [3:0] a = 4'b1010;
wire [3:0] b = 4'b0101;

wire [7:0]  ab  = {a, b};           // Evaluates to 8'b1010_0101
wire [11:0] abc = {a, b, 4'h3};     // Concatenates three distinct fields
wire [8:0]  apb = {1'b0, a, b};     // Prepends zero bit for width alignment

You can also place concatenation on the left side of an assignment to unpack a wide bus:

wire [7:0] byte_val;
wire [3:0] hi, lo;

assign {hi, lo} = byte_val;    // Assigns hi = byte_val[7:4] and lo = byte_val[3:0]

In sequential procedural blocks, concatenation enables single-statement variable swapping:

always @(posedge clk) begin
    {b_reg, a_reg} <= {a_reg, b_reg};
end

Concatenation and part-select operations require zero physical logic gates. Tools resolve these connections entirely through netlist routing.

Vector Replication

The replication operator {N{expr}} repeats an expression N times inside an outer pair of braces:

wire [7:0]  all_ones  = {8{1'b1}};     // Resolves to 8'hFF
wire [15:0] doubled   = {2{data}};     // Duplicates vector data twice
wire [31:0] quad      = {4{8'hAB}};    // Resolves to 32'hABABABAB

Use replication to implement explicit two’s-complement sign extension:

wire signed [7:0]  narrow = 8'sb1010_1100;                 // Signed decimal -84
wire signed [15:0] wide   = {{8{narrow[7]}}, narrow};       // Resolves to 16'hFFAC (-84)
wire        [15:0] wide_u = {8'h00, narrow};                // Zero-extended: 16'h00AC (172)

RISC-V Instruction Field Packing Example

Concatenation and part-select operators pack and unpack complex binary formats such as RISC-V R-type instructions:

// Bit layout: [31:25] funct7 | [24:20] rs2 | [19:15] rs1 | [14:12] funct3 | [11:7] rd | [6:0] opcode

wire [6:0] opcode = 7'b011_0011;    // Register-register instruction opcode
wire [4:0] rd     = 5'd10;
wire [2:0] funct3 = 3'b000;         // ADD instruction identifier
wire [4:0] rs1    = 5'd5;
wire [4:0] rs2    = 5'd6;
wire [6:0] funct7 = 7'b000_0000;    // Standard arithmetic modifier

// Pack instruction fields into 32-bit word: 7 + 5 + 5 + 3 + 5 + 7 = 32 bits
wire [31:0] instruction = {funct7, rs2, rs1, funct3, rd, opcode};

// Unpack instruction fields using part-select operators
wire [6:0] dec_funct7 = instruction[31:25];
wire [4:0] dec_rs2    = instruction[24:20];
wire [4:0] dec_rs1    = instruction[19:15];
wire [2:0] dec_funct3 = instruction[14:12];
wire [4:0] dec_rd     = instruction[11:7];
wire [6:0] dec_opcode = instruction[6:0];

This instruction decoding logic infers zero physical logic gates. The circuit consists entirely of direct wire routing.

Practice Exercise

Design an 8-bit variable cyclic left rotator using only continuous assignment statements. Rotating 8'b1010_0101 left by 1 bit produces 8'b0100_1011 (the MSB wraps around to become the LSB).

Solution
`timescale 1ns / 1ps
`default_nettype none

module rotate_left_8 (
    input  wire [7:0] data,
    input  wire [2:0] amt,
    output wire [7:0] result
);
    // Duplicate the vector and select a sliding 8-bit window
    wire [15:0] doubled = {data, data};
    assign result = doubled[7 + amt -: 8];    // 8 bits selected downward from (7 + amt)
endmodule

When amt equals 0, the window selects bits [7:0]. When amt equals 1, the window selects bits [8:1], which evaluates to {data[6:0], data[7]}. The indexed part-select operator -: extracts the wrapped bits using a single expression.

The next article examines the conditional operator ?: and its mapping to multiplexer hardware primitives.

← Bitwise and Reduction Operators The Conditional Operator →