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.
Every expression has a width and a type (signed vs unsigned). Synthesis follows width rules โ surprises here become silent bugs in silicon.
Verilog groups operators by what they operate on. Confusing & (bitwise) with && (logical) is a classic beginner mistake โ they are not interchangeable.
| Family | Examples | Operands | Result width |
|---|---|---|---|
| Arithmetic | + - * / % | Vectors (unsigned by default) | Max operand width (after extension) |
| Bitwise | & | ^ ~ | Vectors, bit-by-bit | Max operand width |
| Logical | && || ! | Any โ treated as true/false | Always 1 bit |
| Relational | == != < <= > >= | Vectors compared | Always 1 bit |
| Shift | << >> <<< >>> | Vector + shift amount | Left operand width |
| Reduction | & | ^ ~& ~| ~^ (unary) | Single vector | Always 1 bit |
| Concat / replicate | {a,b} {4{x}} | Expressions | Sum of part widths |
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.
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.
Indexing and slicing โ the bread and butter of datapath RTL.
| Syntax | Meaning | Width of result |
|---|---|---|
bus[i] | Single bit at index i | 1 |
bus[hi:lo] | Contiguous slice (inclusive) | hi - lo + 1 |
bus[i+:w] | Part-select from i, width w, ascending | w |
bus[i-:w] | Part-select from i, width w, descending | w |
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)
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}.
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
Mixing signed and unsigned silently promotes to unsigned in many tools. Mark ports signed or use $signed() when doing two's-complement math.
| Expression | a=4'b1010, b=4'b0011 | Notes |
|---|---|---|
a & b | 4'b0010 | Bitwise AND per bit |
a && b | 1'b1 | Both non-zero โ true |
a | b | 4'b1011 | Bitwise OR |
a == b | 1'b0 | Vector equality โ 1 bit |
a === b | 4-state compare | Simulation only โ avoid in RTL |
// 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
<< and >> are logical shifts (zero-fill). <<< and >>> are arithmetic when the left operand is signed (sign-extend on right shift).
| Operator | Name | Fill bits | Typical use |
|---|---|---|---|
<< | Logical left | โ | Multiply by 2n |
>> | Logical right | 0 | Unsigned divide by 2n |
>>> | Arithmetic right | MSB (sign) | Signed divide by 2n |
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
Curly braces build wider vectors. Order is MSB on the left: {a,b} puts a in the upper bits.
{4{byte}} replicates byte four times โ 32-bit bus.
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
{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.
Unary reduction ops fold an entire vector to a single bit โ perfect for any/all style flags.
| Op | Meaning | Example: x=4'b1010 |
|---|---|---|
&x | AND all bits | 0 |
|x | OR all bits | 1 |
^x | XOR all bits (parity) | 1 |
~&x | NAND reduction | 1 |
Combine everything: slice fields, concat a header, use reduction for parity.
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
{a,b} and predict the bit width before sim& vs && on the same inputs{a,b} concatenates MSB-left; {n{x}} replicates.