Logical Operators
Teil des Abschnitts Grundlagen der Verilog-Journey von Coddy. Lektion 24 von 90.
Logische Operatoren arbeiten mit ganzen Werten und geben ein einzelnes Ergebnis zurück: 1 (wahr) oder 0 (falsch). Im Gegensatz zu bitweisen Operatoren, die Bit für Bit arbeiten, behandeln logische Operatoren den gesamten Wert entweder als wahr (non-zero) oder als falsch (zero).
| Typ | Beispiel | Ergebnis |
|---|---|---|
| Bitweises AND | 4'b1010 & 4'b1100 | 4'b1000 (multiple bits) |
| Logisches AND | (4'b1010 && 4'b1100) | 1 (single bit) |
Verfügbare logische Operatoren
| Operator | Bedeutung | Beschreibung |
|---|---|---|
&& | logical AND | Wahr, wenn both Operanden wahr sind (non-zero) |
|| | logical OR | Wahr, wenn mindestens one Operand wahr ist |
! | logical NOT | Wahr, wenn der Operand falsch ist (null) |
So funktionieren sie
Logisches AND (<strong>&&</strong>):
(5 && 3) // 1 (beide ungleich null)
(5 && 0) // 0 (zweites ist null)
(0 && 0) // 0 (beide null)Logisches OR (<strong>||</strong>):
(5 || 3) // 1 (mindestens einer ungleich null)
(5 || 0) // 1 (erster ungleich null)
(0 || 0) // 0 (beide null)Logisches NOT (<strong>!</strong>):
!5 // 0 (Nicht-Null wird false)
!0 // 1 (Null wird true)Codebeispiel
module logical_demo;
reg [3:0] a, b;
reg and_res, or_res, not_res;
initial begin
a = 5;
b = 0;
and_res = (a && b); // 5 && 0 = 0
or_res = (a || b); // 5 || 0 = 1
not_res = !a; // !5 = 0
$display("5 && 0 = %d", and_res);
$display("5 || 0 = %d", or_res);
$display("!5 = %d", not_res);
$finish;
end
endmoduleAusgabe:
5 && 0 = 0
5 || 0 = 1
!5 = 0Häufige Verwendung
Logische Operatoren werden in if-Anweisungen und Bedingungen verwendet:
if (a && b) // Wahr, wenn sowohl a als auch b ungleich null sind
$display("Both true");
if (a || b) // Wahr, wenn mindestens einer ungleich null ist
$display("At least one true");
if (!reset) // Wahr, wenn reset 0 ist
$display("Reset is inactive");Aufgabe
Schreibe die richtigen logischen Ausdrücke für jede Aufgabe.
Aufgabe:
- Check, ob
value1UNDvalue2beide wahr sind, und speichere das Ergebnis inand_out - Check, ob
value1ODERvalue2wahr ist, und speichere das Ergebnis inor_out - Check, ob
value1falsch ist, und speichere das Ergebnis innot_out
Probier es selbst
module logical_challenge;
reg [3:0] value1, value2;
reg and_out, or_out, not_out;
initial begin
value1 = 4'd12;
value2 = 4'd5;
and_out = ______; // value1 && value2
or_out = ______; // value1 || value2
not_out = ______; // !value1
$display("%d && %d = %d", value1, value2, and_out);
$display("%d || %d = %d", value1, value2, or_out);
$display("!%d = %d", value1, not_out);
$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