MATLAB Function Reference
  Go to function:
    Search    Help Desk 
Logical Operators & | ~    Examples   See Also

Logical operations

Syntax

Description

The symbols &, |, and ~ are the logical operators and, or, and not. They work element-wise on arrays, with 0 representing logical false (F), and anything nonzero representing logical true (T). The & operator does a logical and, the| operator does a logical or, and ~A complements the elements of A. The function xor(A,B) implements the exclusive or operation. Truth tables for these operators and functions follow.

Inputs
and
or
xor
not
A
B
A&B
A|B
xor(A,B)
~A
0
0
0
0
0
1
0
1
0
1
1
1
1
0
0
1
1
0
1
1
1
1
0
0

The logical operators have the lowest precedence, with arithmetic operators and relational operators being evaluated first.

The precedence for the logical operators with respect to each other is:

   1.
not has the highest precedence.
   2.
and and or have equal precedence, and are evaluated from left to right.

Remarks

The logical operators have M-file function equivalents, as shown:

and
A&B
and(A,B)
or
A|B
or(A,B)
not
~A
not(A)

Examples

Here are two scalar expressions that illustrate precedence relationships for arithmetic, relational, and logical operators:

They evaluate to 1 and 0 respectively, and are equivalent to:

Here are two examples that illustrate the precedence of the logical operators to each other:

See Also

The relational operators: <, <=, >, >=, ==, ~=, as well as:

all         Test to determine if all elements are nonzero

any         Test for any nonzeros

find        Find indices and values of nonzero elements

logical     Convert numeric values to logical

xor         Exclusive or



[ Previous | Help Desk | Next ]