Reduction Operators
Part of the Fundamentals section of Coddy's Verilog journey — lesson 25 of 90.
Reduction operators work on all bits of a single vector and reduce them to a single bit result. Unlike bitwise operators that compare two numbers bit-by-bit, reduction operators take one number and perform an operation across all its bits to produce a single result.
| Operator | Operation | Result |
|---|---|---|
& | Reduction AND | 1 if all bits are 1 |
| | Reduction OR | 1 if at least one bit is 1 |
^ | Reduction XOR | 1 if odd number of bits are 1 |
~& | Reduction NAND | 0 if all bits are 1 |
~| | Reduction NOR | 0 if at least one bit is 1 |
~^ | Reduction XNOR | 1 if even number of bits are 1 |
How They Work
Reduction AND (<strong>&</strong>):
&4'b1111 = 1 // all bits are 1
&4'b1011 = 0 // not all bits are 1
&4'b0000 = 0 // all bits are 0Reduction OR (<strong>|</strong>):
|4'b0000 = 0 // no bits are 1
|4'b0100 = 1 // at least one bit is 1
|4'b1111 = 1 // all bits are 1Reduction XOR (<strong>^</strong>):
^4'b1010 = 0 // two 1's (even) → 0
^4'b1000 = 1 // one 1 (odd) → 1
^4'b1111 = 0 // four 1's (even) → 0Code Example
module reduction_demo;
reg [3:0] a, b, c;
reg and_red, or_red, xor_red;
initial begin
a = 4'b1111;
b = 4'b1010;
c = 4'b1000;
and_red = &a; // 1111 → 1
or_red = |b; // 1010 → 1
xor_red = ^c; // 1000 → 1
$display("&4'b1111 = %d", and_red);
$display("|4'b1010 = %d", or_red);
$display("^4'b1000 = %d", xor_red);
$finish;
end
endmoduleOutput:
&4'b1111 = 1
|4'b1010 = 1
^4'b1000 = 1Common Uses
Check if all bits are 1:
all_ones = &data; // 1 if data == 8'b11111111Check if any bit is 1:
any_one = |data; // 1 if data != 0Check parity (odd number of 1's):
odd_parity = ^data; // 1 if odd number of 1'sCheck if all bits are 0:
all_zeros = ~|data; // 1 if data == 0Challenge
Write the correct reduction expressions for each task.
What to do:
- Check if all bits of
aare 1 and store inall_ones - Check if any bit of
bis 1 and store inany_one - Check if
chas an odd number of 1's and store inodd_parity
Cheat sheet
Reduction operators work on all bits of a single vector and reduce them to a single bit result.
| Operator | Operation | Result |
|---|---|---|
& | Reduction AND | 1 if all bits are 1 |
| | Reduction OR | 1 if at least one bit is 1 |
^ | Reduction XOR | 1 if odd number of bits are 1 |
~& | Reduction NAND | 0 if all bits are 1 |
~| | Reduction NOR | 0 if at least one bit is 1 |
~^ | Reduction XNOR | 1 if even number of bits are 1 |
Common uses:
all_ones = &data; // 1 if all bits are 1
any_one = |data; // 1 if any bit is 1 (data != 0)
odd_parity = ^data; // 1 if odd number of 1's
all_zeros = ~|data; // 1 if data == 0Try it yourself
module reduction_challenge;
reg [3:0] a, b, c;
reg all_ones, any_one, odd_parity;
initial begin
a = 4'b1111;
b = 4'b0100;
c = 4'b1011;
all_ones = ______; // all bits 1?
any_one = ______; // any bit 1?
odd_parity = ______; // odd number of 1's?
$display("&4'b1111 = %d", all_ones);
$display("|4'b0100 = %d", any_one);
$display("^4'b1011 = %d", odd_parity);
$finish;
end
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic