C # Enum Flags Comparison

Given the following flags,

  [Flags]
    public enum Operations
    {
        add = 1,
        subtract = 2,
        multiply = 4,
        divide = 8,
        eval = 16,
    }

How can I implement an IF condition to perform each operation? In my attempt, the first condition is true for add, eval , which is true. However, the first condition is also true for subtracting eval , which is not true.

        public double Evaluate(double input)
    {
        if ((operation & (Operations.add & Operations.eval)) == (Operations.add & Operations.eval))
            currentResult += input;
        else if ((operation & (Operations.subtract & Operations.eval)) == (Operations.subtract & Operations.eval))
            currentResult -= input;
        else
            currentResult = input;

        operation = null;

        return currentResult;
    }

I don’t see what the problem is.

+5
source share
5 answers

Change your internal &to |:

if ((operation & (Operations.add | Operations.eval)) == (Operations.add | Operations.eval))

This is equivalent to:

if( ((operation & Operations.add)==Operations.add) &&
    ((operation & Operations.eval)==Operations.eval))

which may be more readable. You can also consider the extension as follows:

public static bool HasFlag(this Operations op, Operations checkflag)
{
    return (op & checkflag)==checkflag;
}

You can do it:

if(operation.HasFlag(Operations.add) && Operations.HasFlag(Operations.eval))

which may be even more readable. Finally, you can create this extension for even more fun:

public static bool HasAllFlags(this Operations op, params Operations[] checkflags)
{
    foreach(Operations checkflag in checkflags)
    {
        if((op & checkflag)!=checkflag)
            return false;
    }
    return true;
}

Then your expression may turn into:

if(operation.HasAllFlags(Operations.add, Operations.eval))
+23

, .

, . ( ):

1 in binary is  00001
16 in binary is 10000

  00001
& 10000
--------
  00000

, , (2) operation

2 in binary is     00010
previous result is 00000

  00010
& 00000
--------
  00000

00000, , AND'd . true 0 == 0.

OR, :

1 in binary is  00001
16 in binary is 10000

  00001
| 10000
--------
  10001 (17)

, , Add (1) operation

1 in binary is     00001
previous result is 10001 (17)

  00001
& 10001
--------
  00001

, 1 & 17 => 1 , , (1 & ( 1 | 16 ) ) == ( 1 | 16 ) = > 1 & 17 == 17 = > 1 == 17 = > false ( false!)

:

((operation | Operations.add | Operations.eval) & (Operations.add | Operations.eval)) == (Operations.add | Operations.eval)

((1 | 1 | 16 ) & ( 1 | 16 )) == ( 1 | 16 ) = > ( 17 & 17 ) == 17 = > 17 == 17 == true

, , , ( ). , .

+11
+4

, , , . (Operations.add & Operations.eval) . . - , :

public double Evaluate(double input)
{
    if ((operation & (Operations.add | Operations.eval)) == (Operations.add | Operations.eval))
        currentResult += input;
    else if ((operation & (Operations.subtract | Operations.eval)) == (Operations.subtract | Operations.eval))
        currentResult -= input;
    else
        currentResult = input;

    operation = null;

    return currentResult;
}
+1

:

   public double Evaluate(double input)
{
    if ((operation & (Operations.add | Operations.eval)) == (Operations.add | Operations.eval))
        currentResult += input;
    else if ((operation & (Operations.subtract | Operations.eval)) == (Operations.subtract | Operations.eval))
        currentResult -= input;
    else
        currentResult = input;

    operation = null;

    return currentResult;
}
+1

All Articles