I have a very basic question related to boolean logic.
I have two boolean flags- flagA
and flagB
. I need to calculate flagC
based on the values of flagA
and flagB
.
The code/rules are:
if($flagA && $flagB) {
$flagC = true;
} else if (!$flagA || !$flagB) {
$flagC = false;
} else if(!$flagA && !$flagB) {
$flagC = true;
}
These rules match with the XNOR truth table - http://en.wikipedia.org/wiki/XNOR_gate
I want to find out different ways to re-write the above code(if possible) with:
- fewer lines of code
- better performance (even if it is a minute difference)
- using bit shifting?
The languages I am hoping to write this in - php, ruby/ruby on rails. Any help/pointers will be great! Thanks!