Reduction Operators
Teil des Abschnitts Grundlagen der Verilog-Journey von Coddy. Lektion 25 von 90.
Reduktionsoperatoren arbeiten mit all bits eines einzelnen Vektors und reduzieren sie auf ein Ergebnis aus single bit. Im Gegensatz zu bitweisen Operatoren, die zwei Zahlen Bit für Bit vergleichen, nehmen Reduktionsoperatoren eine Zahl und führen eine Operation über alle ihre Bits aus, um ein einzelnes Ergebnis zu erzeugen.
| Operator | Operation | Ergebnis |
|---|---|---|
& | Reduktions-AND | 1, wenn all bits 1 sind |
| | Reduktions-OR | 1, wenn at least one bit 1 ist |
^ | Reduktions-XOR | 1, wenn eine odd number von Bits 1 ist |
~& | Reduktions-NAND | 0, wenn all bits 1 sind |
~| | Reduktions-NOR | 0, wenn at least one bit 1 ist |
~^ | Reduktions-XNOR | 1, wenn eine gerade Anzahl von Bits 1 ist |
So funktionieren sie
Reduktions-UND (<strong>&</strong>):
&4'b1111 = 1 // all bits are 1
&4'b1011 = 0 // nicht alle Bits sind 1
&4'b0000 = 0 // all bits are 0Reduktion ODER (<strong>|</strong>):
|4'b0000 = 0 // no bits are 1
|4'b0100 = 1 // mindestens ein Bit ist 1
|4'b1111 = 1 // all bits are 1Reduktions-XOR (<strong>^</strong>):
^4'b1010 = 0 // two 1's (even) → 0
^4'b1000 = 1 // one 1 (odd) → 1
^4'b1111 = 0 // vier Einsen (gerade) → 0Codebeispiel
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
endmoduleAusgabe:
&4'b1111 = 1
|4'b1010 = 1
^4'b1000 = 1Häufige Verwendungen
Prüfen, ob alle Bits 1 sind:
all_ones = &data; // 1 wenn data == 8'b11111111Prüfe, ob ein Bit 1 ist:
any_one = |data; // 1 wenn data != 0Parität prüfen (ungerade Anzahl von 1en):
odd_parity = ^data; // 1 bei ungerader Anzahl von 1enPrüfe, ob alle Bits 0 sind:
all_zeros = ~|data; // 1 wenn data == 0Aufgabe
Schreibe die korrekten Reduktionsausdrücke für jede Aufgabe.
Was zu tun ist:
- Prüfe, ob alle Bits von
a1 sind, und speichere das Ergebnis inall_ones - Prüfe, ob irgendein Bit von
b1 ist, und speichere das Ergebnis inany_one - Prüfe, ob
ceine ungerade Anzahl von 1's enthält, und speichere das Ergebnis inodd_parity
Probier es selbst
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
endmoduleDiese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.
Alle Lektionen in Grundlagen
1Einführung
Was ist VerilogHardware vs. SoftwareIhr erstes ModulKommentareAbstraktionsebenen des Designs4Operators 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Übe selbstständig: Online-Verilog-Compiler