Question

What is the opposite of (previous = 4 || previous = 5) && current = 2?

I know I can just write it as !((previous = 4 || previous = 5) && current = 2), but now it's turned into something that will hunt me until I find the answer. How do you distribute the negative within the parenthesis?

This is what I'm thinking but it doesn't seem right to me.

!((previous = 4 || previous = 5) && current = 2

!(previous = 4 || previous = 5) || current != 2

(previous != 4 && previous != 5) || current != 2

Was it helpful?

Solution

DeMorgan's Law states that when you take the complement of an expression (that is, when you negate it), you can simply swap all OR's and AND's and negate every term to get the desired result.

For example

!(A && B) = !A || !B

!(A || B) = !A && !B

!(A && !B) = !A || B

therefore, applying these two simple rules,

!((previous = 4 || previous = 5) && current = 2) 
=
!(previous = 4 || previous = 5) || current != 2
=
(previous != 4 && previous != 5) || current != 2

(broken up over multiple lines because one line was too long)

Note that above, because we had an expression as one of the terms, we applied DeMorgan's Law recursively (twice, to be exact).

Some more examples

!(A > B || A + B == 0) = A <= B && A + B != 0

!(A >= 2 && A < 8) = A < 2 || A >= 8

OTHER TIPS

You can get the answer using this equation !(a && b) = !a || !b

So step by step:

!((previous = 4 || previous = 5) && current = 2)
!(previous = 4 || previous = 5) || !(current = 2)
(previous != 4 && previous != 5) || current != 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top