<p>I have a flag enum below.</p>

<pre><code>[Flags]
public enum FlagTest
{
    None = 0x0,
    Flag1 = 0x1,
    Flag2 = 0x2,
    Flag3 = 0x4
}
</code></pre>

<p>I cannot make the if statement evaluate to true.</p>

<pre><code>FlagTest testItem = FlagTest.Flag1 | FlagTest.Flag2;

if (testItem == FlagTest.Flag1)
{
    // Do something,
    // however This is never true.
}
</code></pre>

<p>How can I make this true?</p>

[{'answer_id': 40213, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 3, 'selected': False, 'text': '<p>For bit operations, you need to use bitwise operators.</p>\n\n<p>This should do the trick:</p>\n\n<pre><code>if ((testItem &amp; FlagTest.Flag1) == FlagTest.Flag1)\n{\n    // Do something,\n    // however This is never true.\n}\n</code></pre>\n\n<p><strong>Edit:</strong>  Fixed my if check - I slipped back into my C/C++ ways (thanks to Ryan Farley for pointing it out)</p>\n'}, {'answer_id': 40217, 'author': 'Scott Nichols', 'author_id': 4299, 'author_profile': 'https://Stackoverflow.com/users/4299', 'pm_score': 8, 'selected': False, 'text': "<pre><code>if ((testItem &amp; FlagTest.Flag1) == FlagTest.Flag1)\n{\n     // Do something\n}\n</code></pre>\n\n<p><code>(testItem &amp; FlagTest.Flag1)</code> is a bitwise AND operation.</p>\n\n<p><code>FlagTest.Flag1</code> is equivalent to <code>001</code> with OP's enum. Now let's say <code>testItem</code> has Flag1 and Flag2 (so it's bitwise <code>101</code>):</p>\n\n<pre><code>  001\n &amp;101\n ----\n  001 == FlagTest.Flag1\n</code></pre>\n"}, {'answer_id': 40218, 'author': 'Damian', 'author_id': 3390, 'author_profile': 'https://Stackoverflow.com/users/3390', 'pm_score': 3, 'selected': False, 'text': '<pre><code>if((testItem &amp; FlagTest.Flag1) == FlagTest.Flag1) \n{\n...\n}\n</code></pre>\n'}, {'answer_id': 40221, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 2, 'selected': False, 'text': '<p>Try this:\n<pre><code>\nif ((testItem &amp; FlagTest.Flag1) == FlagTest.Flag1)\n{\n    // do something\n}\n</code></pre>Basically, your code is asking if having both flags set is the same as having one flag set, which is obviously false.  The code above will leave only the Flag1 bit set if it is set at all, then compares this result to Flag1.</p>\n'}, {'answer_id': 40239, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 4, 'selected': False, 'text': '<p>I set up an extension method to do it: <a href="https://stackoverflow.com/questions/7244/anyone-know-a-good-workaround-for-the-lack-of-an-enum-generic-constraint">related question</a>.</p>\n\n<p>Basically:</p>\n\n<pre><code>public static bool IsSet( this Enum input, Enum matchTo )\n{\n    return ( Convert.ToUInt32( input ) &amp; Convert.ToUInt32( matchTo ) ) != 0;\n}\n</code></pre>\n\n<p>Then you can do:</p>\n\n<pre><code>FlagTests testItem = FlagTests.Flag1 | FlagTests.Flag2;\n\nif( testItem.IsSet ( FlagTests.Flag1 ) )\n    //Flag1 is set\n</code></pre>\n\n<hr>\n\n<p>Incidentally the convention I use for enums is singular for standard, plural for flags. That way you know from the enum name whether it can hold multiple values.</p>\n'}, {'answer_id': 40268, 'author': 'Martin Clarke', 'author_id': 2422, 'author_profile': 'https://Stackoverflow.com/users/2422', 'pm_score': 3, 'selected': False, 'text': "<p>Regarding the edit. You can't make it true. I suggest you wrap what you want into another class (or extension method) to get closer to the syntax you need. </p>\n\n<p>i.e. </p>\n\n<pre><code>public class FlagTestCompare\n{\n    public static bool Compare(this FlagTest myFlag, FlagTest condition)\n    {\n         return ((myFlag &amp; condition) == condition);\n    }\n}\n</code></pre>\n"}, {'answer_id': 1769763, 'author': 'Phil Devaney', 'author_id': 88468, 'author_profile': 'https://Stackoverflow.com/users/88468', 'pm_score': 9, 'selected': True, 'text': '<p>In .NET 4 there is a new method <a href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag%28VS.100%29.aspx" rel="noreferrer">Enum.HasFlag</a>. This allows you to write:</p>\n\n<pre><code>if ( testItem.HasFlag( FlagTest.Flag1 ) )\n{\n    // Do Stuff\n}\n</code></pre>\n\n<p>which is much more readable, IMO.</p>\n\n<p>The .NET source indicates that this performs the same logic as the accepted answer:</p>\n\n<pre><code>public Boolean HasFlag(Enum flag) {\n    if (!this.GetType().IsEquivalentTo(flag.GetType())) {\n        throw new ArgumentException(\n            Environment.GetResourceString(\n                "Argument_EnumTypeDoesNotMatch", \n                flag.GetType(), \n                this.GetType()));\n    }\n\n    ulong uFlag = ToUInt64(flag.GetValue()); \n    ulong uThis = ToUInt64(GetValue());\n    // test predicate\n    return ((uThis &amp; uFlag) == uFlag); \n}\n</code></pre>\n'}, {'answer_id': 1769814, 'author': 'Sekhat', 'author_id': 1610, 'author_profile': 'https://Stackoverflow.com/users/1610', 'pm_score': 6, 'selected': False, 'text': '<p>For those who have trouble visualizing what is happening with the accepted solution\n(which is this),</p>\n\n<pre><code>if ((testItem &amp; FlagTest.Flag1) == FlagTest.Flag1)\n{\n    // Do stuff.\n}\n</code></pre>\n\n<p><code>testItem</code> (as per the question) is defined as,</p>\n\n<pre><code>testItem \n = flag1 | flag2  \n = 001 | 010  \n = 011\n</code></pre>\n\n<p>Then, in the if statement, the left hand side of the comparison is,</p>\n\n<pre><code>(testItem &amp; flag1) \n = (011 &amp; 001) \n = 001\n</code></pre>\n\n<p>And the full if statement (that evaluates to true if <code>flag1</code> is set in <code>testItem</code>),</p>\n\n<pre><code>(testItem &amp; flag1) == flag1\n = (001) == 001\n = true\n</code></pre>\n'}, {'answer_id': 3412480, 'author': 'Leonid', 'author_id': 372909, 'author_profile': 'https://Stackoverflow.com/users/372909', 'pm_score': 4, 'selected': False, 'text': '<p>One more piece of advice... Never do the standard binary check with the flag whose value is "0". Your check on this flag will always be true.</p>\n\n<pre><code>[Flags]\npublic enum LevelOfDetail\n{\n    [EnumMember(Value = "FullInfo")]\n    FullInfo=0,\n    [EnumMember(Value = "BusinessData")]\n    BusinessData=1\n}\n</code></pre>\n\n<hr>\n\n<p>If you binary check input parameter against FullInfo - you get:</p>\n\n<pre><code>detailLevel = LevelOfDetail.BusinessData;\nbool bPRez = (detailLevel &amp; LevelOfDetail.FullInfo) == LevelOfDetail.FullInfo;\n</code></pre>\n\n<p>bPRez will always be true as ANYTHING &amp; 0 always == 0.</p>\n\n<hr>\n\n<p>Instead you should simply check that the value of the input is 0:</p>\n\n<pre><code>bool bPRez = (detailLevel == LevelOfDetail.FullInfo);\n</code></pre>\n'}, {'answer_id': 7164314, 'author': 'Chuck Dee', 'author_id': 275594, 'author_profile': 'https://Stackoverflow.com/users/275594', 'pm_score': 5, 'selected': False, 'text': '<p>@phil-devaney</p>\n\n<p>Note that except in the simplest of cases, the <a href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag%28VS.100%29.aspx" rel="noreferrer">Enum.HasFlag</a> carries a heavy performance penalty in comparison to writing out the code manually.  Consider the following code:</p>\n\n<pre><code>[Flags]\npublic enum TestFlags\n{\n    One = 1,\n    Two = 2,\n    Three = 4,\n    Four = 8,\n    Five = 16,\n    Six = 32,\n    Seven = 64,\n    Eight = 128,\n    Nine = 256,\n    Ten = 512\n}\n\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        TestFlags f = TestFlags.Five; /* or any other enum */\n        bool result = false;\n\n        Stopwatch s = Stopwatch.StartNew();\n        for (int i = 0; i &lt; 10000000; i++)\n        {\n            result |= f.HasFlag(TestFlags.Three);\n        }\n        s.Stop();\n        Console.WriteLine(s.ElapsedMilliseconds); // *4793 ms*\n\n        s.Restart();\n        for (int i = 0; i &lt; 10000000; i++)\n        {\n            result |= (f &amp; TestFlags.Three) != 0;\n        }\n        s.Stop();\n        Console.WriteLine(s.ElapsedMilliseconds); // *27 ms*        \n\n        Console.ReadLine();\n    }\n}\n</code></pre>\n\n<p>Over 10 million iterations, the HasFlags extension method takes a whopping 4793 ms, compared to the 27 ms for the standard bitwise implementation. </p>\n'}, {'answer_id': 33811816, 'author': 'Waleed A.K.', 'author_id': 347374, 'author_profile': 'https://Stackoverflow.com/users/347374', 'pm_score': 1, 'selected': False, 'text': '<p>even without [Flags], you could use something like this</p>\n\n<pre><code>if((testItem &amp; (FlagTest.Flag1 | FlagTest.Flag2 ))!=0){\n//..\n}\n</code></pre>\n\n<p>or if you have a Zero value enum</p>\n\n<pre><code>if((testItem &amp; (FlagTest.Flag1 | FlagTest.Flag2 ))!=FlagTest.None){\n//..\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/40211', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2469/']