Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to simply resemble a truth table?

Tags:

c#

Is there any simple way to resemble a truth table in code? It has 2 inputs and 4 outcomes, as shown below:

enter image description here

My current code is:

private void myMethod(bool param1, bool param2)
{
    Func<int, int, bool> myFunc;
    if (param1)
    {
        if (param2)
            myFunc = (x, y) => x >= y;
        else
            myFunc = (x, y) => x <= y;
    }
    else
    {
        if (param2)
            myFunc = (x, y) => x < y;
        else
            myFunc = (x, y) => x > y;
    }
    //do more stuff
}
like image 659
Aviran Katz Avatar asked Sep 12 '16 10:09

Aviran Katz


People also ask

How do you represent a truth table?

A truth table is a mathematical table used to determine if a compound statement is true or false. In a truth table, each statement is typically represented by a letter or variable, like p, q, or r, and each statement also has its own corresponding column in the truth table that lists all of the possible truth values.

How are truth tables used in real life?

We can use truth tables to determine if the structure of a logical argument is valid.To tell if the structure of a logical argument is valid, we first need to translate our argument into a series of logical statements written using letters and logical connectives.


1 Answers

I suggest using an array, i.e.

  // XOR truth table
  bool[][] truthTable = new bool[][] {
    new bool[] {false, true},
    new bool[] {true, false},
  };

...

  private void myMethod(bool param1, bool param2, bool[][] table) {
    return table[param1 ? 0 : 1][param2 ? 0 : 1];
  }   
like image 71
Dmitry Bychenko Avatar answered Oct 17 '22 04:10

Dmitry Bychenko