Menu
Coddy logo textTech

Logical Operators

Part of the Fundamentals section of Coddy's Verilog journey — lesson 24 of 90.

Logical operators work on whole values and return a single result: 1 (true) or 0 (false). Unlike bitwise operators that work bit-by-bit, logical operators treat the entire value as either true (non-zero) or false (zero).

TypeExampleResult
Bitwise AND4'b1010 & 4'b11004'b1000 (multiple bits)
Logical AND(4'b1010 && 4'b1100)1 (single bit)

Available Logical Operators

OperatorMeaningDescription
&&Logical ANDTrue if both operands are true (non-zero)
||Logical ORTrue if at least one operand is true
!Logical NOTTrue if operand is false (zero)

How They Work

Logical AND (<strong>&&</strong>):

(5 && 3)   // 1 (both non-zero)
(5 && 0)   // 0 (second is zero)
(0 && 0)   // 0 (both zero)

Logical OR (<strong>||</strong>):

(5 || 3)   // 1 (at least one non-zero)
(5 || 0)   // 1 (first non-zero)
(0 || 0)   // 0 (both zero)

Logical NOT (<strong>!</strong>):

!5         // 0 (non-zero becomes false)
!0         // 1 (zero becomes true)

Code Example

module logical_demo;
  reg 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

Output:

5 && 0 = 0
5 || 0 = 1
!5     = 0

Common Use

Logical operators are used in if statements and conditions:

if (a && b)           // True if both a and b are non-zero
  $display("Both true");

if (a || b)           // True if at least one is non-zero
  $display("At least one true");

if (!reset)           // True when reset is 0
  $display("Reset is inactive");
challenge icon

Challenge

Write the correct logical expressions for each task.

What to do:

  1. Check if value1 AND value2 are both true and store in and_out
  2. Check if value1 OR value2 is true and store in or_out
  3. Check if value1 is false and store in not_out

Cheat sheet

Logical operators treat entire values as true (non-zero) or false (zero), returning a single bit result.

OperatorMeaningResult
&&Logical AND1 if both operands are non-zero
||Logical OR1 if at least one operand is non-zero
!Logical NOT1 if operand is zero
(5 && 3)  // 1
(5 && 0)  // 0
(5 || 0)  // 1
(0 || 0)  // 0
!5        // 0
!0        // 1

Compared to bitwise operators, logical operators collapse the whole value:

4'b1010 &  4'b1100  // 4'b1000 (bitwise, multiple bits)
4'b1010 && 4'b1100  // 1       (logical, single bit)

Commonly used in conditions:

if (a && b)   // true if both non-zero
if (a || b)   // true if at least one non-zero
if (!reset)   // true when reset is 0

Try it yourself

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
endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals