# Logical Operators

We will now define the logical operators which we mentioned earlier, using truth tables. But let us proceed with caution: most of the operators have names which we may be accustomed to using in ways that are fuzzy or even contradictory to their proper definitions. In all cases, use the truth table for an operator as its exact and only definition; try not to bring to logic the baggage of your colloquial use of the English language.

The first logical operator which we will discuss is the "AND", or conjunction operator. For the computer scientist, it is perhaps the most useful logical operator we will discuss. It is a "binary" operator (a binary operator is defined as an operator that takes two operands; not binary in the sense of the binary number system):

p AND q

It is traditionally represented using the following symbol:

But we will represent it using the ampersand ("&") since that is the symbol most commonly used on computers to represent a logical AND. It has the following truth table:

 p q p & q T T T T F F F T F F F F

Notice that p & q is only T if both p and q are T. Thus the rigorous definition of AND is consistent with its colloquial definition. This will be very useful for us when we get to Boolean Algebra: there, we will use 1 in place of T and 0 in place of F, and the AND operator will be used to "mask" bits.

Perhaps the quintessential example of masking which you will encounter in your further studies is the use of the "network mask" in networking. An IP ("Internet Protocol") address is 32 bits long, and the first n bits are usually used to denote the "network address", while the remaining 32 – n bits denote the "host address":

Suppose that on your network, the three most significant bits in the first byte of an IP address denote the network address, while the remaining 29 bits of the address are used for the host. To find the network address, we can AND the first byte with

1 1 1 0 0 0 0 02

since

x x x y y y y y

` &`

1 1 1 0 0 0 0 0

` =`

x x x 0 0 0 0 0

(x & 1 = x, but x & 0 = 0). Thus masking allows the system to separate the network address from the host address in order to identify which network information is to be sent to. Note that most network numbers have more than 3 bits. You will spend a lot of time working with network masks in your courses on networking.

The OR (or disjunction) operator is also a binary operator, and is traditionally represented using the following symbol:

We will represent OR using the stroke ("|"), again due to common usage on computers. It has the following truth table:

 p q p | q T T T T F T F T T F F F

p | q is true whenever either p is true, q is true or both p and q are true (so it too agrees with its colloquial counterpart).

The NOT (or negation or inversion) operator is a "unary" operator: it takes just one operand, like the unary minus in arithmetic (for instance, -x). NOT is traditionally represented using either the tilde ("~") or the following symbol:

In a programming environment, NOT is frequently represented using the exclamation point ("!"). Since the exclamation point is too easy to mistake for the stroke, we will use the tilde instead. Not has the following truth table:

 p ~ p T F F T

~ p is the negation of p, so it again agrees with its colloquial counterpart; it is essentially the 1’s complement operation.

The XOR (eXclusive OR) operator is a binary operator, and is not independent of the operators we have presented thus far (many texts do not introduce it as a separate logical operator). It has no traditional notation, and is not often used in programming (where our usual logical operator symbols originate), so we will simply adopt the "X" as the symbol for the XOR:

 p q p X q T T F T F T F T T F F F

p X q is T if either p is T or q is T, but not both. We will see later how to write it in terms of ANDs, ORs and NOTs.

XOR has a number of specific and essentially unrelated uses: for instance, in random numbers, in cryptography and in computer graphics. One common usage is as the quickest way to assign the value zero: p X p is always zero.

The implication operator (IMPLIES) is a binary operator, and is defined in a somewhat counterintuitive manner (until you appreciate it, that is!). It is traditional notated by one of the following symbols:

but we will denote it with an arrow (""):

 p q p → q T T T T F F F T T F F T

So p → q follows the following reasoning:

1. a True premise implies a True conclusion, therefore T → T is T;
2. a True premise cannot imply a False conclusion, therefore T → F is F; and
3. you can conclude anything from a false assumption, so F → anything is T.

IMPLIES (implication) is definitely one to watch; while its definition makes sense (after a bit of thought), it is probably not what you are used to thinking of as implication.

EQUIVALENCE is our final logical operator; it is a binary operator, and is traditionally notated by either an equal sign, a three-lined equal sign or a double arrow (""):

 p q p ↔ q T T T T F F F T F F F T

p ↔ q is T if p and q are the same (are equal), so it too follows our colloquial notion.

Just as with arithmetic operators, the logical operators follow "operator precedence" (an implicit ordering). In an arithmetic expression with sums and products and no parentheses, the multiplications are performed before the additions. In a similar fashion, if parentheses are not used, the operator precedence for logical operators is:

1. First do the NOTs;
2. then do the ANDs;
3. then the ORs and XORs, and finally
4. do the IMPLIES and EQUIVALENCEs.

Needless to say, it will behove you to use parentheses whenever you have any doubts!

Please visit our website at www.saypro.online Email: info@sayro.online Call: + 27 (0) 11 071 1903 Email: info@saypro.online Tel: + 27 11 071 1903 WhatsApp: + 27 84 313 7407. Comment below for any questions and feedback. For SayPro Courses, SayPro Jobs, SayPro Community Development, SayPro Products, SayPro Services, SayPro Consulting, and SayPro Advisory visit our website to www.saypro.online

Comment below or leave your enquiry, question, feedback below