Reputation:
I am reading a C book and don't understand this statement it is asking me to evaluate:
!(1 && !(0 || 1))
I can understand some things here... This is what I have so far, not(1 and not(0 or 1))
So is it not 1 and not 0 or 1
? Or is it not 1 and 0 or 1
? Do those two !
cancel each other out like a double negative? The answer is true
but I expected false
.
What is the explanation?
Upvotes: 3
Views: 859
Reputation: 7917
Use De Morgan's laws to simplify the original expression: !(1 && !(0 || 1))
. When you negate a parenthetical logical expression, the negation is applied to each operand and the operator is changed.
!(1 && !(0 || 1)) // original expression
!1 || !!(O || 1) // by De Morgan's law
!1 || (0 || 1) // the two !!'s in front of the second operand cancel each other
0 || (0 || 1) // !1 is zero
0 || 1 // the second operand, 0 || 1, evaluates to true because 1 is true
1 // the entire expression is now 0 || 1, which is true
The answer is true.
A couple of other answers have said that the parentheses determine order of evaluation. That is wrong. In C, precedence is not the same as order of evaluation. Precedence determines which operands are grouped by which operators. The exact order of evaluation is unspecified. The logical operators are an exception: they are evaluated in strictly left-to-right order in order to enable short-circuit behavior.
Upvotes: 5
Reputation: 117
If A =1 A && B = B. So the final expression inside !(....) is !(!(0 || 1)) which is 0 || 1 and 0 + 1 =1, hence the answer is true.
Upvotes: 2
Reputation: 1222
!(1 && !(0 || 1)) => !(1 && !(1)) => !(1 && !1) => !(1 && 0) => !(0) => !0 => 1(true)
Upvotes: 2
Reputation: 18750
(0 || 1) == 1
!1 == 0
1 && 0 == 0
!0 == 1
also known as true :) Keep in mind that ||
and &&
are short circuit operators, but in this case you still have to evaluate the right side because the operators do not short circuit
Upvotes: 3
Reputation: 31
!(1 && !(0 || 1)) => not(1 and not(0 or 1))
not(1 and not(0 or 1)) => not(1 and (0 and 1)) // !(a or b) = a and b
not(1 and (0 and 1)) => not(0) => 1 => true
Upvotes: 3
Reputation: 19830
It easy if you remember about which operation order
!(1 && !(0 || 1)) = !(1 && !(1)) = !(1 && 0) = !(0) = 1
Upvotes: 1
Reputation: 6565
(0 || 1) --> 1
! 1 --> 0
1 && 0 --> 0
! 0 -- > 1
Answer true
Upvotes: 1
Reputation: 122373
!(1 && !(0 || 1))
since 0 || 1
evaluates as 1
, is the same as
!(1 && !1)
continue
!(1 && 0)
continue
!0
so it's 1
, true.
Upvotes: 1
Reputation: 175
To evaluate this, start with the innermost parentheses and work your way out like so:
not(1 and not(0 or 1)) -> not(1 and not(1)) -> not(1 and 0) -> not(0) -> 1 -> true.
Upvotes: 1
Reputation: 6044
!(1 && !(0 || 1))
=> ! (1 && !(1)) //0 || 1 is 1
=> ! (1 && 0) // !1 is 0
=> !0 // 1 && 0 is 0
=> 1 // !0 is 1
=>true
Upvotes: 2
Reputation: 180867
Starting from the deepest nesting and working outwards adding piece by piece;
(0 || 1) = (0 OR 1) = 1
!(0 || 1) = !1 = NOT 1 = 0
1 && !(0 || 1) = 1 && 0 = 1 AND 0 = 0
!(1 && !(0 || 1)) = !0 = NOT 0 = 1
Upvotes: 1