user2677183
user2677183

Reputation:

Evaluation of this statement

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

Answers (11)

verbose
verbose

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

pukingminion
pukingminion

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

EnterKEY
EnterKEY

Reputation: 1222

!(1 && !(0 || 1))  =>  !(1 && !(1)) =>  !(1 && !1)  => !(1 && 0) => !(0) => !0 => 1(true) 

Upvotes: 2

aaronman
aaronman

Reputation: 18750

  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

Upvotes: 3

Saroop G
Saroop G

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

Piotr Stapp
Piotr Stapp

Reputation: 19830

It easy if you remember about which operation order

 !(1 && !(0 || 1)) = !(1 && !(1)) = !(1 && 0) = !(0) = 1

Upvotes: 1

Pandiyan Cool
Pandiyan Cool

Reputation: 6565

(0 || 1) --> 1

! 1      --> 0

1 && 0   --> 0

! 0     -- > 1

Answer true

Upvotes: 1

Yu Hao
Yu Hao

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

Jlewis071
Jlewis071

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

mohit
mohit

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

Joachim Isaksson
Joachim Isaksson

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

Related Questions