Question

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?

Was it helpful?

Solution 2

  1. (0 || 1) == 1
  2. !1 == 0
  3. 1 && 0 == 0
  4. !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

OTHER TIPS

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.

!(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
   !(1 && !(0 || 1))
=> ! (1 && !(1))     //0 || 1 is 1
=> ! (1 && 0)        // !1 is 0
=> !0                // 1 && 0 is 0
=> 1                 // !0 is 1
=>true
!(1 && !(0 || 1))  =>  !(1 && !(1)) =>  !(1 && !1)  => !(1 && 0) => !(0) => !0 => 1(true) 

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.

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

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.
!(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.

(0 || 1) --> 1

! 1      --> 0

1 && 0   --> 0

! 0     -- > 1

Answer true

It easy if you remember about which operation order

 !(1 && !(0 || 1)) = !(1 && !(1)) = !(1 && 0) = !(0) = 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top