# Logical Operators & | ~

## Purpose

Logical operations.

## Synopsis

    A & B
    A | B
    ~A

## Description

The symbols `&`, `|`, and `~` are the logical operators AND, OR, and NOT. They work element-wise on matrices, with 0 representing FALSE and anything nonzero representing TRUE. `A & B` does a logical AND, `A | B` does a logical OR, and `~A` complements the elements of `A`. The function `xor(A,B)` implements the exclusive OR operation.

    Inputs    AND    OR    XOR

     0  0      0      0     0
     0  1      0      1     1
     1  0      0      1     1
     1  1      1      1     0

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

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.

## Examples

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

    1 & 0 + 3
    3 > 4 & 1

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

    1 & (0 + 3)
    (3 > 4) & 1

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

    1 | 0 & 0 = 0
    0 & 0 | 1 = 1

## See Also

     <, <=, >, >=, ==, ~=, all, any, find, xor

(c) Copyright 1994 by The MathWorks, Inc.