Simply put, is there and operator for the AND OR concept?
EX: if (a = true AND OR b = true)
If not an operator, what process could be done to do this?
Simply put, is there and operator for the AND OR concept?
EX: if (a = true AND OR b = true)
If not an operator, what process could be done to do this?
The English concept and/or
is already represented in C-based languages as ||
, which is inclusive.
By that, I mean a || b
is true if a
is true, or b
is true or both are true.
The "you are a boy or a girl" meaning of "or" (either "you are a boy" or "you are a girl", but not both) is exclusive, and is not really catered for with primitive operators.
To achieve that, you could use an expression like:
a != b
assuming they were values generated by a Boolean operation, rather than arbitrary integers (which C will also allow).
For treating arbitrary integers the same way C does, use:
!!a != !!b
The double Boolean negation will force them to be "proper" Boolean values before comparison.
If you want a more in-depth examination of the different bitwise operators, see this answer, or refer to the following tables, where F
indicates false (or zero) and T
represents true (or one):
AND | F T OR | F T XOR | F T NOT | F T
----+----- ---+---- ----+---- ----+----
F | F F F | F T F | F T | T F
T | F T T | T T T | T F
In most programming languages, including C and C++, logical OR (||
) takes its meaning from mathematics, and is therefore inclusive: either A or B is true, or both are true.
In English, we say “and/or” because “or” is typically exclusive, akin to logical XOR: either A or B is true, but not both.
Most languages lack a logical XOR operator, because it’s equivalent to not-equals (!=
) for Booleans.
bool xor(bool a, bool b) {
return a != b;
}
Did you mean XOR (Exclusive OR)?
The short answer for what the XOR operator is: ^
But from the looks of it, I think you may need a stronger foundation of how variables are compared, you may want to get familiar with this: http://www.cplusplus.com/doc/tutorial/operators/
Saying if A is true AND OR B is true isn't a correct programming expression nor is it a correct expression in Boolean algebra. You cant have a conjuncture and disjuncture like this in a statement. You cant represent that statement in a Venn diagram or a logic gate, and you should be able to do both with any valid Boolean expression. To see this try and write your question in a truth table.