Accelerating the pace of engineering and science

• Trials

# Logical Operators: Elementwise & | ~

Elementwise logical operations on arrays

expr1 & expr2
expr1 | expr2
~expr

## Description

The symbols &, |, and ~ are the logical array operators AND, OR, and NOT. These operators are commonly used in conditional statements, such as if and while, to determine whether or not to execute a particular block of code. Logical operations return a logical array with elements set to 1 (true) or 0 (false), as appropriate.

expr1 & expr2 represents a logical AND operation between values, arrays, or expressions expr1 and expr2. In an AND operation, if expr1 is true and expr2 is true, then the AND of those inputs is true. If either expression is false, the result is false. Here is a pseudocode example of AND:

```IF (expr1: all required inputs were passed) AND ...
(expr2: all inputs are valid)
THEN (result: execute the function)```

expr1 | expr2 represents a logical OR operation between values, arrays, or expressions expr1 and expr2. In an OR operation, if expr1 is true or expr2 is true, then the OR of those inputs is true. If both expressions are false, the result is false. Here is a pseudocode example of OR:

```IF (expr1: S is a string) OR ...
(expr2: S is a cell array of strings)
THEN (result: parse string S)
```

~expr represents a logical NOT operation applied to expression expr. In a NOT operation, if expr is false, then the result of the operation is true. If expr is true, the result is false. Here is a pseudocode example of NOT:

```IF (expr: function returned a Success status) is NOT true
THEN (result: throw an error)```

The function xor(A,B) implements the exclusive OR operation.

### Logical Operations on Arrays

The expression operands for AND, OR, and NOT are often arrays of nonsingleton dimensions. When this is the case, The MATLAB® software performs the logical operation on each element of the arrays. The output is an array that is the same size as the input array or arrays.

If just one operand is an array and the other a scalar, then the scalar is matched against each element of the array. When the operands include two or more nonscalar arrays, the sizes of those arrays must be equal.

This table shows the output of AND, OR, and NOT statements that use scalar and/or array inputs. In the table, S is a scalar array, A is a nonscalar array, and R is the resulting array:

OperationResult
S1 & S2R = S1 & S2
S & AR(1) = S & A(1); ...
R(2) = S & A(2); ...
A1 & A2R(1) = A1(1) & A2(1);
R(2) = A1(2) & A2(2); ...
S1 | S2R = S1 | S2
S | AR(1) = S | A(1);
R(2) = S | A(2); ...
A1 | A2R(1) = A1(1) | A2(1);
R(2) = A1(2) | A2(2); ...
~SR = ~S
~AR(1) = ~A(1);
R(2) = ~A(2), ...

### Compound Logical Statements

The number of expressions that you can evaluate with AND or OR is not limited to two (e.g., A & B). Statements such as the following are also valid:

`expr1 & expr2 & expr3 | expr4 & expr5`

Use parentheses to establish the order in which MATLAB evaluates a compound operation. Note the difference in the following two statements:

```(expr1 & expr2) | (expr3 & expr4)   % 2-component OR
expr1 & (expr2 | expr3) & expr4    % 3-component AND```

### Operator Precedence

The precedence for the logical operators with respect to each other is shown in the table below. MATLAB always gives the & operator precedence over the | operator. Although MATLAB typically evaluates expressions from left to right, the expression a|b&c is evaluated as a|(b&c). It is a good idea to use parentheses to explicitly specify the intended precedence of statements containing combinations of & and |.

Operator

Operation

Priority

~

NOT

Highest

&

Elementwise AND

|

Elementwise OR

&&

Short-circuit AND

||

Short-circuit OR

Lowest

### Short-Circuiting in Elementwise Operators

The &, and |operators do not short-circuit. See the documentation on the && and || operators if you need short-circuiting capability.

When used in the context of an if or while expression, and only in this context, the elementwise & and | operators use short-circuiting in evaluating their expressions. That is, A&B and A|B ignore the second operand, B, if the first operand, A, is sufficient to determine the result.

So, although the statement 1|[] evaluates to false, the same statement evaluates to true when used in either an if or while expression:

```A = 1;   B = [];
if(A|B) disp 'The statement is true',  end;
The statement is true```

while the reverse logical expression, which does not short-circuit, evaluates to false:

`if(B|A) disp 'The statement is true',  end;`

Another example of short-circuiting with elementwise operators shows that a logical expression such as the one shown below, which under most circumstances is invalid due to a size mismatch between A and B, works within the context of an if or while expression:

The A|B statement generates an error:

```A = [1 1];   B = [2 0 1];
A|B

Error using  |
Inputs must have the same size.```

But the same statement used to test an if condition does not error:

```if (A|B) disp 'The statement is true',  end;
The statement is true```

### Operator Truth Table

The following is a truth table for the operators and functions in the previous example.

Inputsandornotxor
ABA & BA | B~Axor(A,B)
000010
010111
100101
111100

### Equivalent Functions

These logical operators have function equivalents, as shown here.

Logical Operation

Equivalent Function

A & B

and(A,B)

A | B

or(A,B)

~A

not(A)

## Examples

### Example 1 — Conditional Statement with OR

Using OR in a conditional statement, call function parseString on S, but only if S is a character array or a cell array of strings:

```if ischar(S) | iscellstr(S)
parseString(S)
end```

### Example 2 — Array AND Array

Find those elements of array R that are both greater than 0.3 AND less then 0.9:

```rng(0,'twister');
R=rand(5,7)
R =
0.8147    0.0975    0.1576    0.1419    0.6557    0.7577    0.7060
0.9058    0.2785    0.9706    0.4218    0.0357    0.7431    0.0318
0.1270    0.5469    0.9572    0.9157    0.8491    0.3922    0.2769
0.9134    0.9575    0.4854    0.7922    0.9340    0.6555    0.0462
0.6324    0.9649    0.8003    0.9595    0.6787    0.1712    0.0971
(R > 0.3) & (R < 0.9)
ans =
1     0     0     0     1     1     1
0     0     0     1     0     1     0
0     1     0     0     1     1     0
0     0     1     1     0     1     0
1     0     1     0     1     0     0```

### Example 3 — Array AND Scalar

Find those elements of array R that are greater than the scalar value of 40:

```rng(0,'twister');
R = rand(3,5) * 50
R =
40.7362   45.6688   13.9249   48.2444   47.8583
45.2896   31.6180   27.3441    7.8807   24.2688
6.3493    4.8770   47.8753   48.5296   40.0140
R > 40
ans =
1     1     0     1     1
1     0     0     0     0
0     0     1     1     1
```

### Example 4 — Check Status with NOT

Throw an error if the return status of a function does NOT indicate success:

```[Z, status] = myfun(X, Y);
if ~(status == SUCCESS);
error('Error in function myfun')
end```

### Example 5 — OR of Binary Arrays

This example shows the logical OR of the elements in the vector u with the corresponding elements in the vector v:

```u = [0 0 1 1 0 1];
v = [0 1 1 0 0 1];
u | v

ans =
0   1   1   1   0   1```