Operator Types

Logical Operators

Keysymbols: &&, ||, !.

The logical operators are logical and, logical or and logical not.All logical operators evaluate to either true ( 1 ), false ( 0 ), or unknown ( x ). An operand is true if it is non zero, and false if it is zero. An unknown or a high impedance value evaluates as false. An operand can be a variable or an expression which evaluates to either true or false as defined above.

        module logicalTest;

           reg [3:0] a, b, c;

           initial begin
              a = 2; b = 0; c = 4'hx;

              $display(a && b); // logical and, evaluates to 0
              $display(a || b); // logical or, evaluates to 1
              $display(!a); // logical not, evaluates to 0
              $display(a || c); // evaluates to 1, unknown || 1 (=1)
              $display(!c); // evalutes to unknown
              end

        endmodule // logicalTest

EXERCISE
What do the following evaluate to ?
a) !(67)
b) 0 && 1;
c) 1 || 2;
d) 0 || (1 &&1) || !1
e) What value of gamma is displayed if the following module is run ?
        module testlogical;
           integer alpha, beta, gamma;

           initial begin
              alpha = 1'b0;
              beta = 1'b0;
              gamma = 1'b0;
              alpha = !beta;
              gamma = alpha || beta;
              $display("gamma = %d", gamma);
           end
        endmodule // testlogical

Answers


previous next contents