-3

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!

user_stackoverflow
  • 734
  • 2
  • 10
  • 18
  • 2
    Your rules are for AND, not XNOR. Which one do you want? – JJJ Nov 07 '13 at 22:01
  • Yes you are right Juhana. By mistake, I ignored a rule completely. My rules are for AND not XNOR. – user_stackoverflow Nov 07 '13 at 22:45
  • However, the above question arose when I wanted to implement the rules as per XNOR. So I am still curious to know the answers. I updated the rule in the question accordingly. – user_stackoverflow Nov 07 '13 at 22:48
  • 1
    In that case, I think my answer below is a pretty simple implementation. – Derek Nov 07 '13 at 22:51
  • 1
    The logic in the question is still wrong (the first and second ifs cover all possibilities so the third one can never be reached). The logic for XNOR is `c = ( a && b ) || ( !a && !b )`. – JJJ Nov 08 '13 at 08:15
  • $flagC = true; before the comparisons. – Wayne Nov 08 '13 at 17:44

2 Answers2

1

Don't use these languages much but this might work:

$flagC = ($flagA == $flagB);

From the link posted: http://en.wikipedia.org/wiki/XNOR_gate

two-input version implements logical equality, behaving according to the truth table to the right. A HIGH output (1) results if both of the inputs to the gate are the same. If one but not both inputs are HIGH (1), a LOW output (0) results.

So flagC is true when flagA equals flagB.

Derek
  • 7,615
  • 5
  • 33
  • 58
  • `a == b` is not the same as `a && b`. – JJJ Nov 07 '13 at 21:58
  • If you look at the link posted: http://en.wikipedia.org/wiki/XNOR_gate XNOR is true when the two inputs are equal. From the link: two-input version implements logical equality, behaving according to the truth table to the right. A HIGH output (1) results if both of the inputs to the gate are the same. If one but not both inputs are HIGH (1), a LOW output (0) results. – Derek Nov 07 '13 at 21:59
  • Right, but the code in the question is for AND. Must wait for the OP to clarify. – JJJ Nov 07 '13 at 22:02
  • hmm... guess I am too trusting :) – Derek Nov 07 '13 at 22:04
  • Derek, you answered after "understanding" the question with a workable answer. If I could hire additional people to work on programming projects I would pick you. I one upped it with $FlagC = $flagA - $flagB; to be faster, but your answer is more understandable. and maintains type casting, true === true, which is highly desirable to avoid strange behaviors. Your answer also works in platforms that do not have logical operators. – Wayne Nov 08 '13 at 17:30
0
if($flagA && $flagB) {
      $flagC = true; 
    } else {
      $flagC = false; 
    }

(Your second rule covers all other cases.)

lincb
  • 622
  • 5
  • 20