Simplify "all or nothing" boolean expression - boolean-logic

Can a "all or nothing" boolean expression be simplified? Suppose I have three values, A, B, C, and want to determine if all three are true, or all three are false. Like a XOR gate, but with N values.
Can this statement be simplified?
(A && B && C) || !(A || B || C)

All true or all false basically means that all should be the same. So if the equality comparison is acceptable you can do this:
A == B && B == C

Related

Boolean Algebra expression won't simplify consistently?

I got this question on one of my exams recently and it's been annoying me ever since.
¬¬(A.B).¬B + ¬¬¬C
on the exam I put that
A.B.¬B + ¬C = 0 + ¬C = ¬C and left it there, and some of the online calculators I put it through give this answer, but others give a different answer e.g.
¬¬(A.B).¬B + ¬¬¬C
¬¬(A.B).¬B + ¬C
¬¬A.B + ¬B + ¬C
AB + ¬B + ¬C
A + ¬B + ¬C
and some classmates got this. Both methods seem correct, what am I missing?
If by this:
¬¬(A.B).¬B + ¬¬¬C
You understand this:
(((not not (A and B)) and (not B)) or (not not not C))
Then the simplest possible form we can get at is derived as follows:
(((not not (A and B)) and (not B)) or (not not not C))
<=> (((A and B) and (not B)) or (not C))
<=> (((A and (not B)) AND (B and (not B))) or (not C))
<=> (((A and (not B)) AND false) or (not C)
<=> (false or (not C))
<=> not C
In the other derivation you give, the mistake is going from the 2nd line to the 3rd. The equivalence is lost in that step, the expressions are not equivalent. To see this, simply evaluate each expression for the assignment A=true, B=false, C=true and we can verify the first expression is false and the second is true:
((not not (A and B)) and (not B)) or (not C)
<=> ((not not (true and false)) and (not false)) or (not true)
<=> ((not not false) and (not false)) or (not true)
<=> (false and true) or (not true)
<=> false or false
<=> false
but
((not not A) and B) or (not B) or (not C)
<=> ((not not true) and false) or (not false) or (not true)
<=> (true and false) or (not false) or (not true)
<=> false or true or false
<=> true
If there is a rule they were trying to use in that step, they misused it.

Expression (!a && b) || (!a && c) || (b && c) with minimum gates

I want to create a logical circuit
(!a && b) || (!a && c) || (b && c)
using as few logical gates (~ a nand b) nand NOT, AND, OR, NAND, NOR, XOR, NXOR as possible. The gate types can be mixxed. I have found some online calculators that can convert the above expression to NANDs only like
(!a nand b) nand (!a nand c) nand (b nand c)
But I wonder if there is a way to do it by using less than four gates.
Four gates and one inverter seem to be minimal:
This result was created by Logic Friday 1.
Entered:
f = (!a & b) | (!a & c) | (b & c);
Minimized:
f = a' b + a' c + b c;
It seems like it cannot be minimized more than this: a'b + c(a' + b) or can even be written as follows a'(b + c) + bc
which is actually identical to a'b + a'c + bc (has same truth table).

Are there any languages where "A == B == C" works where A, B, and C are all non-boolean types?

Without thinking in C# I tried to compare three objects. It failed, and explained why [since (typeof("A == B") == bool) and (typeof(C) != bool) it was an invalid comparison]. Do any languages support short circuiting logic like this?
There are several languages that let you do multiple conditionals like this.
But the first I could think of was Python
Example:
a = 5
b = 5
c = 5
if(a == b == c):
print "yes"
else:
print "no"
Will print "yes" in the console.
It works with other types as well, like this:
a = ["A",1]
b = ["A",1]
c = ["A",1]
d = ["A",1]
if(a == b == c == d):
print "YES"
else:
print "NO"
Now the reason for C# (And other C like languages) doesn't support this is that they evaluate comparison expressions down to a true / false, so what your compiler sees when you do (5 == 5 == 5) is ((5 == 5) == 5) which yields: (true == 5) which invalid since you cannot compare a Boolean to an integer, you could actually write (a == b == c) if c is a Boolean, so (5 == 5 == true) would work.

Boolean negation

One of my exam questions reads:
! ( ! ( a != b) && ( b > 7 ) )
The choices:
a) (a != b) || (b < 7)
b) (a != b) || (b <= 7)
c) (a == b) || (b <= 7)
d) (a != b) && (b <= 7)
e) (a == b) && (b > 7)
Initially, I thought it would be D. This is incorrect, and I realize why. I don't understand how the logical negation operator reverses && and greater than/less than. I believe I have narrowed it down to the first two. Is there any instance > would change to <= ?
Is there any instance > would change to <= ?
Answer: every time you negate it.
Consider x > 1. The negation of this is clearly x <= 1. If you simply negate it as x < 1 then neither case covers the x == 1 case.
That being said, the given boolean ! ( ! ( a != b) && ( b > 7 ) ) can be decomposed as follows:
Given:
! ( !(a != b) && (b > 7))
Negate a != b:
! ((a == b) && (b > 7))
Distribute the !:
!(a == b) || !(b > 7)
Negate a==b:
(a != b) || !(b > 7)
Negate b>7:
(a != b) || (b <= 7)
The answer is, therefore, B.
The answer should be B. This is because the negation next to the (a != b) is evaluated first, then you distribute the outside negation to the entire proposition.
Using DeMorgan's Laws, the && will switch to ||. Similarly, != becomes ==, and > becomes <=.
!(!(a != b) && (b > 7))
!((a == b) && (b > 7))
(a != b) || (b <= 7)
! ( ! ( a != b) && ( b > 7 ) )
= ! ( (a = b) && (b > 7))
= (a != b) || (b <= 7)
answer is B.
to understand this :
! ( ! ( a != b) && ( b > 7 ) )
Lets break it into parts.
Part dummy: (a!=b)
Part X: !dummy
Part Y: (b>7)
Now !X = double negate of dummy => dummy => (a!=b)
!Y = !(b>7) => b should not be greater than 7 => b should be less than or equal to 7 => (b<=7)
Now problem left is how && becomes ||
So original question is: !( X && Y ) => should not be (X and Y) => it should be either negate of X or it should be negate of Y, because if instead of X it is ~X, the condition (X and Y) becomes false and hence !(X and Y) becomes true and hence original condition is achieved. Similarly for Y.
Firt apply the inner bracket Logical NOT (!):
!(!(a != b) && (b > 7)) becomes !((a == b) && (b > 7))
With De Morgan's Law we reverse the operators to their counterpart.
We change > to <= because the > operator does not include 7 itself or anything less, hence the <= is the only one that satisfies that condition.
Now the outer !:
When looking at truth tables (picture above), you'll notice that Logical AND (&&) and Logical OR (||) have opposite results when comparing 2 different boolean expressions (i.e. truth false, false true), hence when we apply the !, we reverse the && with ||. Finally we need to switch the == to != again.
All up, this produces
((a != b) || (b <= 7))

How to writer the condition A or B is zero , but not both is zero in MYSQL?

What is the correct syntax for the following SQL statement ?
SELECT * FROM TABLE WHERE (A = 0 AND B != 0) OR (A!=0 AND B=0)
Thanks
Updated: How about negative case? I would like to check if A or B is non-zero , but not both is non-zero, Thanks
SELECT OperateProductId FROM DPS_UserLoginStatus Where OperateProductId <> 0 XOR OperateIssueId <> 0
You're looking for the XOR logical operator:
SELECT * FROM TABLE WHERE A = 0 XOR B = 0
In terms of other operators, a XOR b (commonly a ^ b when used bitwise rather than logical), is equivalent to (a and !b) or (b and !a). It's also equivalent to: (a or b) and (!a or !b). In English, it's exactly what you're looking for: a xor b is true if and only if either a or b is true but both are not true.