Logic Operators
Logic operators are operators with one or two logical operands that yield a logical result.
There are 5 binary operators and one unary operator
The general form of a binary operation is
l
1op l
2The general form of unary operator
op l
1Where l
1and l
2are operands (expressions
or variables) and op is the logic operator
Logic Operators (Cont … )
If the relationship between l1 and l2 expressed by the operator is true, then the operation returns a value true (display 1 in the Command Window)
If the relationship between l1 and l2 expressed by the operator is false, then the operation returns a value false (display 0 in the Command Window)
Logic Operators (Cont … )
Operator Operation Binary/Unary
& Logical AND Binary
&& Logical AND with shortcut evaluation Binary
| Logical Inclusive OR Binary
|| Logical Inclusive OR with shortcut evaluation Binary xor Logical Exclusive OR Binary
~ Logical NOT Unary
Logic Operators (Cont … )
Logical ANDs
The result of an AND operator is true if and only if both input operands ar e true. If either or both operands are false, the result is false
&& supports short circuit evaluation ( or partial evaluations) while & does n’t.
&& only works between scalar values, while & works with either scalar or array values, as long as the sizes of the arrays are compatible.
Logical Inclusive ORs
The result of an inclusive OR operator is true if either of the input operan ds are true. If both operands are false, the result is false
|| supports short circuit evaluation (or partial evaluations) while | doesn’t.
|| only works between scalar values, while | works with either scalar or arr ay values, as long as the sizes of the arrays are compatible.
Logic Operators (Cont … )
Logical Exclusive OR (xor)
The result of an exclusive OR is true if and only if one operand is true and t he other one is false. If both the operands are true or both the operands ar e false, the result is false
Example:
>> a=10;
>> b=0;
>>x = xor (a, b);
1 (true)
Logical NOT (~)
The NOT operator is a unary operator, having only one operand. The result s of a NOT operator is true if its operand is false, and false if its operand is true
Truth Tables for Logic Operators
Inputs AND OR xor NOT l1 l2 l1 & l 2 l1 && l2 l1 | l2 l1 || l2 xor (l1, l2) ~l1
F F F F F F F T F T F F T T T T
T F F F T T T F T T T T T T F F
Using Numeric Data with Logic Operators
Real numeric data can be used with logic operators
Since logic operators expect logical input values, MATLAB converts non-zero values to true and zero values to false before performing the operation
Example 1
And display as in the command window
false false
false false b
&
a then
false b
false
false
false a true
If
0 0
0 0
Example 2
Hierarchy of Operations
1. Al arithmetic operators are evaluated accordingly
2. All relational operators (==, ~=, >, >=, <, <=) are evaluated, working from left to right
3. All ~ operators are evaluated
4. All & and && operators are evaluated, working from left to right 5. All | , ||, and xor operators are evaluated, working from left to ri
ght
true false
true b true
| a then
true false
true b true
true
false
false a true
If
Logical Functions
Function Purpose
ischar(a) Returns true if a is a character array and false otherwise isempty(a) Returns true if a is an empty array and false otherwise isinf(a) Returns true if a is infinite and false otherwise
isnan(a) Returns true if a is NaN and false otherwise
isnumeric(a) Returns true if a is a numeric array and false otherwise logical(a) Converts numerical values to logical values
(If a value is non-zero, it is converted to true If a value is zero, it is converted to false)
Class Exercise
Assume a, b, c, and d are as defined, evaluate the following expressions a = 20 b = -2 c = 0 d = 1
1. a > b
1 (logical true)
2. b > d
0 (logical false)
3. a && b > c 0 (logical false)
Class Exercise (Continued ….)
Assume a, b, c, and d are as defined, evaluate the following e xpressions
a = 2;
1. ~ (a > b)
2. c <= d Illegal
The <= operator must be between arrays of the same size,
or between an array and a scalar
0 10 2 b 1
0 1 0
2 1 1 d -
0
2
1 c 0