Logical Operators
Coddy'nin Verilog Journey'sinin Temeller bölümünün bir parçası. Ders 24 / 90.
Mantıksal operatörler tam değerler üzerinde çalışır ve tek bir sonuç döndürür: 1 (true) veya 0 (false). Bit düzeyinde çalışan bitwise operatörlerin aksine, mantıksal operatörler tüm değeri true (non-zero) ya da false (zero) olarak ele alır.
| Tür | Örnek | Sonuç |
|---|---|---|
| Bit düzeyinde AND | 4'b1010 & 4'b1100 | 4'b1000 (multiple bits) |
| Mantıksal AND | (4'b1010 && 4'b1100) | 1 (single bit) |
Kullanılabilir Mantıksal Operatörler
| Operatör | Anlamı | Açıklama |
|---|---|---|
&& | Mantıksal AND | both işlenen doğruysa (sıfır olmayan) doğru |
|| | Mantıksal OR | at least one işlenen doğruysa doğru |
! | Mantıksal NOT | İşlenen yanlışsa (zero) doğru |
Nasıl Çalışırlar
Mantıksal AND (<strong>&&</strong>):
(5 && 3) // 1 (ikisi de sıfır değil)
(5 && 0) // 0 (ikincisi sıfır)
(0 && 0) // 0 (ikisi de sıfır)Mantıksal VEYA (<strong>||</strong>):
(5 || 3) // 1 (en az bir sıfır olmayan)
(5 || 0) // 1 (ilk sıfır olmayan)
(0 || 0) // 0 (ikisi de sıfır)Mantıksal NOT (<strong>!</strong>):
!5 // 0 (sıfır olmayan false olur)
!0 // 1 (sıfır true olur)Kod Örneği
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
endmoduleÇıktı:
5 && 0 = 0
5 || 0 = 1
!5 = 0Yaygın Kullanım
Mantıksal işleçler if ifadelerinde ve koşullarda kullanılır:
if (a && b) // a ve b'nin her ikisi de sıfır değilse doğru
$display("Both true");
if (a || b) // En az biri sıfır değilse doğru
$display("At least one true");
if (!reset) // reset 0 olduğunda doğru
$display("Reset is inactive");Görev
Her görev için doğru mantıksal ifadeleri yazın.
Yapılacaklar:
value1VEvalue2değerlerinin her ikisinin de doğru olup olmadığını kontrol edin ve sonucuand_outiçinde saklayınvalue1VEYAvalue2değerlerinden birinin doğru olup olmadığını kontrol edin ve sonucuor_outiçinde saklayınvalue1değerinin yanlış olup olmadığını kontrol edin ve sonucunot_outiçinde saklayın
Kendin dene
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
endmoduleBu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.
Temeller bölümündeki tüm dersler
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 LogicKendi başına pratik yap: Online Verilog derleyicisi