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

  • SEARCH
  • Home
  • 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 6584959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:36:09+00:00 2026-05-25T16:36:09+00:00

suppose you have enum MyEnum {A = 0, B = 1, C = 2,

  • 0

suppose you have
enum MyEnum {A = 0, B = 1, C = 2, D = 4, E = 8, F = 16};

At some point you have a function that will check an instance of MyEnum and return true if it is C,D, or F

bool IsCDF(MyEnum enumValue) 
{
  return //something slick
}

I remember that there was some really slick way to do bit shifting and preform this operation that read way better than a bunch of ternary if statements but for the life of me I can’t remember what it is.

Anyone know?

  • 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-25T16:36:10+00:00Added an answer on May 25, 2026 at 4:36 pm

    If you make it a [Flags] enum, you can assign a different bit value (1, 2, 4, 8, 16…) to each enumerated value. Then you can use a bitwise operation to determine if a value is one of a set of possible values.

    So, to see if it is C, D, or F:

    bool IsCDF(MyEnum enumValue)
    {
        return ((enumValue & (MyEnum.C | MyEnum.D | MyEnum.F)) != 0);
    }
    

    or using HasFlag() (less efficient but more readable):

    bool IsCDF(MyEnum enumValue)
    {
        return enumValue.HasFlag(MyEnum.C | MyEnum.D | MyEnum.F);
    }
    

    Note that this will not work for a value of 0 (in your example, ‘A’), and you must be careful that all enum values resolve to unique bit values (i.e. non-zero powers of two).

    The advantages of this approach are:

    • it will typically take a single CPU instruction/cycle to execute, whereas doing three separate “if” checks will take 3 or more instructions (depending on your target platform).
    • You can pass the set of values that you want to test with as an enum value (a single integer) instead of needing to use lists of enum values.
    • You can do lots of other useful things with bitwise operations, which would be clunky and slow with ordinary numerical/comparative approaches.

    Handy hint:
    When defining [Flags] enums, use left-shift (<<) to make the bit values clearer (and much harder to get wrong) especially for higher-order bits:

    [Flags]
    enum MyEnum
    {
        A = 1 << 0,     // Equivalent to 1
        B = 1 << 1,     // Equivalent to 2
        C = 1 << 2,     // Equivalent to 4
        D = 1 << 3,     // Equivalent to 8
        …
        Big = 1 << 26,  // Equivalent to 67108864
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have some named enums: enum MyEnum { FOO, BAR = 0x50 };
Suppose that I have an enum column on a table and I want users
Suppose that we have this class: public enum KindOfPerson { Student, Teacher, ... }
suppose I have an enum [Flags] public enum E { zero = 0, one
Suppose I have the following: typedef enum functionType {ln, sin, sqrt} functionType; NSArray *functions
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a data type enum TreeTypes { TallTree, ShortTree, MediumTree } .
So lets suppose we have an action in a controller that looks a bit

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.