Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1071065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:39:38+00:00 2026-05-16T20:39:38+00:00

In this question , I use xor operator between enum with [Flags] attribute as

  • 0

In this question, I use xor operator between enum with [Flags] attribute as following:

[Flags]
enum QueryFlag
{
  None = 0x1,
  ByCustomer = 0x2,
  ByProduct = 0x4,
  ByDate = 0x8
}
QueryFlag flags = QueryFlag.ByCustomer | QueryFlag.ByProduct;

To add an QueryFlag, of course we should use | operator.

flags |= QueryFlag.ByDate;

To remove one, I have a different way with Dan Tao’s answer. I’m using:

flags ^= QueryFlag.ByProduct;

while he is using:

flags &= ~QueryFlag.ByProduct;

Obviously his answer is correct and easy to understand. I thought I made a mistake. But after a deep thought I got:

a,b         a^b         a&(~b)
0,0          0           0
0,1          1           0   //the difference
1,0          1           1
1,1          0           0

And now I knew my mistake. ^ is wrong when you try to remove one item which doesn’t exist.

QueryFlag q = QueryFlag.ByCustomer | QueryFlag.ByDate;
//try to remove QueryFlag.ByProduct which doesn't exist in q
q ^ QueryFlag.ByProduct    //equals to add ByProduct to q, wrong!
q & (~QueryFlag.ByProduct) // q isn't changed, remain the original value. correct!

But here I got another question: how can I know if q contains one item? Base on Dan Tao’s answer I wrote an extension:

public static bool Contains(this QueryFlag flags, QueryFlag flag)
{
   return (flags & (~flag)) != flags;
}

That is, if flags is not changed after removing flag from flags, we know flag is not in flags! It seems correct when:

(QueryFlag.ByProduct | QueryFlag.ByDate).Contains(QueryFlag.None)   //false
(QueryFlag.ByProduct | QueryFlag.ByDate).Contains(QueryFlag.ByDate)  //true

But in fact:

(QueryFlag.ByProduct | QueryFlag.ByDate).Contains(QueryFlag.ByDate | QueryFlag.ByCustomer) //true, but I suppose it's false

I know the reason why it’s false, how can I improve it? It’s the first question.
The second: I want to make the .Contains generic to more enum with [Flags] attribute.

public static bool Contains<T>(this T flags, T flag) where T : Enum//with [Flags]
{
    return (flags & (~flag)) != flags;
}

Probably it’s impossible to constrain T with attribute marked. But even I remove this constraint I get a compile error which says operator ~ can't be applied to type T. Why and how to resolve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:39:39+00:00Added an answer on May 16, 2026 at 8:39 pm

    Your error lies in this method:

    public static bool Contains(this QueryFlag flags, QueryFlag flag)
    {
       return (flags & (~flag)) != flags;
    }
    

    This returns true whenever flags has any (i.e. at least one) of the flags contained in flag, but I think you want all.

    It should read:

    public static bool Contains(this QueryFlag flags, QueryFlag flag)
    {
       return (flags & flag) == flag;
    }
    

    Alternatively, you can just use Enum.HasFlag() which does exactly this. For example:

    QueryFlag qf = ...;
    if (qf.HasFlag(QueryFlag.ByCustomer))
        // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following this question , I use this function to convert arrays to objects, function
Similar, but different to this question : I use highlight occurrences a lot, but
This question originates from a discussion on whether to use SQL ranking functionality or
In this question it has been shown how to use neat boolean variables in
This question is hard to phrase, so I'm going to have to use some
This question may be an absolute crap. Can i use the flex 4 sdk
This question is not about 'best' barcode library recommendation, we use various products on
Kind of like this question I have many text snippets that I use many,
I wanted to use full import names in my project (see this question ).
I have a use case with NServiceBus explained in this question. Essentially there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.