Generate a truth table to simplify code?

So often when you are a programming novice you see code with lots of “if” statements and lots of comparator operators, eg

if(val_1 > val_2 && val_3 < val_4 || val_5 == val_1)
{}

I have started using enums but usually the state of an enum is one value is true the rest are false. eg

enum myEnum
{
 value1;
 value2;
 value3;
 value4;
};

currentState myState = value3;

so value3 will be true/1 and the rest will be false. However if I wanted more than one value to be true like a truth table, eg

value1 = 0;
value2 = 0;
value3 = 1;
value4 = 1;

Can you use enums to achieve this. It would be nice to define a state based on the combination of whats on/off or whats true/false as this would reduce the nasty clunky long arguments of the if statement. eg something like

state1 = [0,0,1,1];
state2 = [0,1,0,1];
state3 = [0,1,1,1];

Where in the code do you propose to do the comparisons? Please give us a complete program to demonstrate more clearly.

so value3 will be true/1 and the rest will be false.

Not true. Testing that the variable myState's value is value3 might be true.

Can you use enums to achieve this.

No.

You could combine the 4 bits now stored in 4 separate elements/variables into one byte, using bitSet() or bit shifting.

byte test = 0;
if(value1 == something)
   bitSet(test, 0);
if(value2 == something)
   bitSet(test, 1);
if(value3 == something)
   bitSet(test, 2);
if(value4 == something)
   bitSet(test, 3);
if(test == 12)
  // bits 3 and 1 are set, meaning that value2 matched and value 4 matched...

romarshmallow:

state1 = [0,0,1,1];

state2 = [0,1,0,1];
state3 = [0,1,1,1];

If I were doing something like that I would treat each position as a different power of 2. Then the states you have listed would be

state1 = 3; // 0 + 0 + 2 + 1
state2 = 5; // 0 + 4 + 0 + 1
state3 = 7; // 0 + 4 + 2 + 1

[1,1,1,1] would be 15 or 8 + 4 + 2 + 1
[0,0,0,0] would be 0

This way the tests are easy and you can calculate the values on the fly if needed.

…R

I am not fond of using enum,always pictured it , perhaps foolishly , as kind of alias allowing assigning a variable (name) to a common variable ( name).

I think trying to "automate/ hide " logical conditions in any scheme is not a good idea.

So many times we see if ( a and b and c and d and e and f and g) written as if (( (a and b) and ( c and d)) and (e and f and g)) just because it "looks logical".

I got nothing against experimenting. but some automation IMHO is a clear example of A.Cooper's "inmates are running the asylum" dancing bear. Just because it can be done is not always the best. And forcing enum into logical true / false is iffy.

Vaclav:
I am not fond of using enum,always pictured it , perhaps foolishly

Flying in the face of established good programming practice?
Yes, I’d say foolish and not much of a “perhaps” about it.

as kind of alias allowing assigning a variable (name) to a common variable ( name).

There are 7 days a week. Doesn't "If today is Thursday" make more sense than "if today is 5"? Or would that be 4? Depending on how you number things? Thursday is definitive, and the actual value irrelevant.

PaulS: byte test = 0; if(value1 == something)   bitSet(test, 0); if(value2 == something)   bitSet(test, 1); if(value3 == something)   bitSet(test, 2); if(value4 == something)   bitSet(test, 3); if(test == 12)  // bits 3 and 1 are set, meaning that value2 matched and value 4 matched...

Try again.