Reputation: 67
In my Be Prepared Comp Sci textbook, I ran into this question:
Assuming that x, y, and z are integer variables, which of the following three logical expressions are equivalent to each other, that is, have equal values for all possible values of x, y, and z?
(x == y && x != z) || (x != y && x == z)
(x == y || x == z) && (x != y || x != z)
(x == y) != (x == z)
A. None of the three
B. I and II only
C. II and III only
D. I and III only
E. I, II, and III
I selected "B", but got it wrong. I really think I need help understanding boolean logic. The correct answer says something else but I don't get the logic. Here is the correct answer:
Expression III is the key to the answer: all three expressions state the fact that exactly one out of two equalities, x == y or x == z, is true.
Expression I states that either the first and not the second or the second and not the first is true.
Expression II states that one of the two is true and one of the two is false.
Expression III simply states that they have different values.All three boil down to the same thing. The answer is E.
Upvotes: 3
Views: 4259
Reputation: 2429
Read the values out loud as if you were writing a sentence.
Note that exclusive or returns true only if one is true, inclusive means one or both values have to be true for it to return true.
In statements in English, exclusive/inclusive 'or's are often implicit. That is, people use 'or' with two different meanings:
Exclusive: I am dead or I am not dead.
Inclusive: Let's go to the park or let's get ice cream.
This is a casual explanation of the two, but I think that's what you want. The first statement necessitates that if one is true, the other is not, so it's an exclusive or. The second is inclusive because going to the park and eating ice cream is possible, but either one alone will suffice.
In my example, OR = inclusive or, XOR = exclusive or.
(x == y && x != z) || (x != y && x == z)
(x is equal to y but not equal to z) OR (x isn't equal to y but is equal to z)
In either case, the statement returns true if and only if x is equal to one of the other variables. If x == y and x == z, then both expressions on either side fail.
(x == y || x == z) && (x != y || x != z)
(x is equal to y OR x is equal to z) AND (x is not equal to y OR x is not equal to z)
The left only evaluates to true when x is equal to one of the two values. The right only evaluates to true if x is not equal to one of the values. The left and the right expression both have to be true for the whole thing to return true because there is an AND between them.
Thus, the equivalent, simplified statement is: x is equal to one of the two values and x is not equal to one of the two values.
(x == y) != (x == z)
This one is the most simple.
(x is equal to y) is not equal to (x is equal to z).
Equivalent statement: (x is equal to y) XOR (x is equal to z)
Or in plain english: either x is equal to y or x is equal to z.
Notice how prepending either to the front of an or statement makes it exclusive:
Either you are dead or you're not dead.
This explicitly signifies an exclusive 'or' rather than an inclusive one.
Upvotes: 3
Reputation: 8856
(x == y && x != z) || (x != y && x == z)
(x == y || x == z) && (x != y || x != z)
(x == y) != (x == z)
Let's break these down.
(x == y && x != z) || (x != y && x == z)
x is equal to y and not z, or x is equal to z and not y
So basically, x is equal to one of [y|z], but y != z
.
(x == y || x == z) && (x != y || x != z)
x is equal to y or x is equal to z, and x is not equal to y or x is not equal to z
This is a bit more complex. It pretty much boils down to x equals one of [y|z] but not the other
, or x is equal to y and not z, or x is equal to z and not y
, the same as the first equation.
This is because (x == y)
will make the first half the equation true, but therefore x != y
must be false in the second half. To keep the second half of the equation true, therefore, x must not equal z. You can flip this logic to mean x == z but x != y if you wish. This boils down to, as mentioned above, x is equal to one of [y|z] but not the other
.
(x == y) != (x == z)
The value of the expression x == y is the opposite of the value of the expression x == z.
This also is a bit complex, but it works out to the same thing. If x equals y, to keep the statement true x must not equal z. Conversely, if x does not equal y, x must equal z. Therefore, x is equal to one of [y|z] but not the other
.
Upvotes: 5