Module 4: Introduction to Verilog HDL · 4.3 Operators

Bitwise and Reduction Operators

Bitwise operators perform logic functions across corresponding bits of two vectors. Unary reduction operators evaluate all bits within a single vector to produce a 1-bit result. Both operator families map directly to basic logic gates arranged in parallel arrays or balanced trees.

Bitwise Operators

Bitwise operators execute bit-by-bit logic operations between equal-width operands:

Operator Name Verilog Symbol Output Bit Equals 1 When
Bitwise AND & Both input bits equal 1
Bitwise OR | Either input bit equals 1
Bitwise XOR ^ Input bits differ
Bitwise XNOR ~^ or ^~ Input bits match
Bitwise NOT ~ Input bit equals 0
wire [7:0] a = 8'b1010_1010;
wire [7:0] b = 8'b1100_1100;

wire [7:0] band  = a & b;       // Evaluates to 8'b1000_1000
wire [7:0] bor   = a | b;       // Evaluates to 8'b1110_1110
wire [7:0] bxor  = a ^ b;       // Evaluates to 8'b0110_0110
wire [7:0] bxnor = a ~^ b;      // Evaluates to 8'b1001_1001
wire [7:0] bnot  = ~a;          // Evaluates to 8'b0101_0101
wire [7:0] bnand = ~(a & b);    // Evaluates to 8'b0111_0111
wire [7:0] bnor  = ~(a | b);    // Evaluates to 8'b0001_0001

Each bit position operates independently. A bitwise AND on a 64-bit vector synthesizes 64 parallel 2-input AND gates. The logic propagation delay remains constant regardless of bus width.

Common Bitwise Masking Operations

Digital hardware uses four primary bitwise masking patterns:

wire [7:0] lower_nibble = data & 8'h0F;      // Mask: retain selected bits; clear remaining bits
wire [7:0] bit7_set     = data | 8'h80;      // Set: force selected bits to 1
wire [7:0] bit0_flip    = data ^ 8'h01;      // Toggle: invert selected bits
wire [7:0] lower_clear  = data & ~(8'h0F);   // Clear: force selected bits to 0

The expression ~(8'h0F) is an elaboration-time constant. Synthesis tools evaluate constant inversions during elaboration without inserting physical inverters.

Unary Reduction Operators

A reduction operator places a bitwise logic symbol as a unary prefix before a single vector. The operator combines all vector bits into a single output bit:

wire [7:0] data = 8'b1010_0101;

wire and_all  = &data;     // 1 if all bits are 1; evaluates to 0
wire nand_all = ~&data;    // 1 if any bit is 0; evaluates to 1
wire or_all   = |data;     // 1 if any bit is 1; evaluates to 1
wire nor_all  = ~|data;    // 1 if all bits are 0; evaluates to 0
wire xor_all  = ^data;     // 1 if an odd number of bits are 1; evaluates to 0
wire xnor_all = ~^data;    // 1 if an even number of bits are 1; evaluates to 1

The expression ^data calculates $d_7 \oplus d_6 \oplus \dots \oplus d_0$. The synthesis tool balances the XOR gates into a binary tree with a depth of $\lceil \log_2 N \rceil$ gate levels. An 8-bit reduction creates 3 gate delays. A 64-bit reduction creates 6 gate delays.

Expression Boolean Meaning Typical Circuit Application
|data Any bit equals 1 Non-zero status detector
~|data All bits equal 0 ALU zero flag output
&data All bits equal 1 Terminal counter state detector
~&data At least one bit equals 0 Incomplete bus fill detector
^data Odd number of set bits Odd parity calculator
~^data Even number of set bits Even parity calculator

An ALU zero flag can use either equality comparison or NOR reduction:

wire zero_a = (result == 8'h00);
wire zero_b = ~|result;

Both expressions infer an identical NOR reduction tree. The reduction syntax clearly expresses the hardware intent.

Parameterized Parity Generator

Even parity adds a parity bit to ensure that the total count of 1 bits remains even. Odd parity ensures that the total count remains odd:

`timescale 1ns / 1ps
`default_nettype none

module parity_gen #(
    parameter WIDTH = 8
)(
    input  wire [WIDTH-1:0] data,
    output wire             parity_even,   // Asserts high when data contains odd count of ones
    output wire             parity_odd     // Complement of even parity
);

    assign parity_even = ^data;    // XOR reduction: 1 when number of active bits is odd
    assign parity_odd  = ~^data;   // XNOR reduction

endmodule

The parity generator synthesizes into a balanced combinational XOR tree. This circuit forms the basis of UART serial frames, error-correcting codes (ECC), and cyclic redundancy checks (CRC).

Hardware Synthesis Summary

Verilog Expression Synthesized Hardware Implementation Propagation Delay
a & b, a | b, a ^ b Parallel array of $N$ 2-input logic gates 1 gate delay
~a Parallel array of $N$ inverters 1 inverter delay
|data, &data, ^data Balanced tree of logic gates $\lceil \log_2 N \rceil$ gate delays
~|data, ~&data, ~^data Balanced tree followed by output inverter $\lceil \log_2 N \rceil + 1$ gate delays

These operators require no clock signals and infer pure combinational logic.

Practice Exercise

Design a 4-bit equality comparator module using only bitwise and reduction operators. Do not use relational equality operators (==).

Solution
`timescale 1ns / 1ps
`default_nettype none

module comparator_4bit (
    input  wire [3:0] a,
    input  wire [3:0] b,
    output wire       eq
);
    wire [3:0] diff = a ^ b;    // Bit equals 0 where a and b contain identical values
    assign eq = ~|diff;         // NOR reduction: 1 when all bits of diff equal 0
endmodule

The bitwise XOR detects differences across all bit positions. The unary NOR reduction asserts high when all difference bits equal zero. This implementation matches the exact gate netlist generated by the relational == operator.

The next article examines shift operators, vector slice rearrangement, and concatenation techniques in bus architecture design.

← Relational and Logical Operators Shift and Concatenation Operators →