Logical operators in ‘C’
Logical operators in ‘C’
There are three types of logical operators.
- logical-AND (&&)
- logical-OR (||)
- logical-NOT (!)
- The logical operators perform logical-AND (&&), logical-OR (||), and logical-NOT (!) operations.
- These are binary operators except NOT (!) because it’s a unary operator.
Truth Table
logical AND operator ( && )
&& → this is the symbol or an operator to perform a logical-AND operation on the operands.
- The logical-AND operator produces the value 1 if both operands have nonzero values.
- If either operand is equal to 0, the result is 0. If the first operand of a logical-AND operation is equal to 0, the second operand is not evaluated.
For example, consider this expression C = (A && B). Here A=-10 and B=20. Here, A is nonzero and B is nonzero. Nonzero means true(1) in ‘C’. A is -10, so true and B is 20, so this operand is also true.
According to the truth table of the logical-AND, A && B, this expression will be evaluated.
The truth table of AND, OR, and NOT is shown in Figure 2.
If both operands are true, the result of the AND operation will be true. That’s why, A&&B this expression will be true. So, C = 1.
Now, A=0 and B=20. What is the result of A&&B?
A is 0 means A is false. So, false. If one operand is false, you need not to check another operand. Because the result is anyway false, that’s why it will be straight away false. So, C will be 0.
Now, A= 10, B=0, then what happens. A is true. So, if A is true, then only the B operand will be checked. In this case, it is 0, which means false. So, the result of the expression will be false.
Remember that when you are using logical operators, you have to talk in terms of logical states like true(1) and false(0) of the variables or the expression. You should not consider the real value of the variables or the expressions.
logical OR Operator ( || )
- The logical-OR operator performs an inclusive-OR operation on its operands.
- The result is 0 if both operands have 0 values according to the truth table.
- If either operand has a nonzero value, the result is 1.
- If the first operand of a logical-OR operation has a nonzero value, the second operand is not evaluated.
logical NOT operator ( ! )
- It reverses the state of the operand.
- If a condition or expression is true, then Logical NOT operator will make it false.
For Example, A = 1, B = -5 and you have one expression like C = !(A == B). This is the combination of relational operator and logical operator. What is the value of C in this Expression?
C = !(A == B)
C= !(1 == -5)
C = !(0)
C = 1
First, the parenthesis will be evaluated. !(A == B), this is a relational operator. So, which is false. That’s why it will be 0. !(0), which is not of False. So, not of false is true. So, 1 will be stored here. C = 1.
Now let’s take another example.
Find out the x value in this x=!x expression?
Here x=10.
x is a non-zero value, so the state of the x is true(1). Not of true means, which is false. So here !x is 0. So, 0 will be stored in x. Result is x = 0.
Let’s take another example. The expression is A = !((x>5) && (y<5)); Here x=7, y=9. What is the value of A in the below expression?
First of all, parentheses will be evaluated first here. So, from left to right. x is greater than 5 (x>5), which is true(1); this is a relational operator. y is less than 5 (y < 5); it is false. That will be 0. So, A = !(1 && 0).
A = !(1 && 0) it is logical AND Operation. Truth(1) and False(0) is False(0), look at the Truth table(Figure 3). So the result will be false(0).
Then, A= !(0). Not of 0 means 1. A will be 1 in this case.
In the following article, let’s understand the decision making in ‘C’.
Get the Microcontroller Embedded C Programming Full Course on Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1