• 검색 결과가 없습니다.

Logic Operators

N/A
N/A
Protected

Academic year: 2021

Share "Logic Operators"

Copied!
11
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

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

1

op l

2

The general form of unary operator

op l

1

Where l

1

and l

2

are operands (expressions

or variables) and op is the logic operator

(2)

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)

(3)

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

(4)

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.

(5)

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

(6)

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

(7)

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

(8)

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

(9)

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)

(10)

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)

(11)

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

1 0

0

0

참조

관련 문서

A cone is called strongly convex if it does not contain a nontrivial linear subspace. In this paper, every cone is assumed to be strongly convex. A polyhedral cone is

• A legal assignment for the circuit, is an assignment of values to the labeled wires where the output value of each multiplication gate is indeed the product of the

‰ a digital circuit is combinational if its output values only depend on its (current)

 If we wish to develop a learning process based on

 In order to handle sequence of random numbers for a certain particle simulation, it is required to set a seed number to a prescribed value. Especially, this adjustment is

Conversely, if the optimal objective value of the phase I problem is 0, each artificial variable should be 0 and the values of the remaining variables form a feasible solution

- if a series has only positive real number or zero, it must be convergent or divergent (not oscillatory). - if a series

Inductance and stray capacitance raises the impedance (AC resistance) of the closed switch. The cap won’t instantly discharge. Worse, depending on the physical arrangement