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 914575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:44:26+00:00 2026-05-15T17:44:26+00:00

Let’s say I have the following int susan = 2; //0010 int bob =

  • 0

Let’s say I have the following

int susan = 2; //0010
int bob = 4; //0100
int karen = 8; //1000

and I pass 10 (8 + 2) as a parameter to a method and I want to decode this to mean susan and karen

I know that 10 is 1010

but how can I do some logic to see if a specific bit is checked as in

if (condition_for_karen) // How to quickly check whether effective karen bit is 1

Right now all i can think of is to check whether the number i passed is

14 // 1110
12 // 1100
10 // 1010
8 //  1000

When I have a larger number of actual bits in my real world scenario, this seems impractical, what is a better way using a mask to just check whether or not I meet the condition for just karen?

I can think of shifting left then back then shifting right then back to clear bits other than the one I’m interested in, but this also seems overly complex.

  • 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-15T17:44:26+00:00Added an answer on May 15, 2026 at 5:44 pm

    The traditional way to do this is to use the Flags attribute on an enum:

    [Flags]
    public enum Names
    {
        None = 0,
        Susan = 1,
        Bob = 2,
        Karen = 4
    }
    

    Then you’d check for a particular name as follows:

    Names names = Names.Susan | Names.Bob;
    
    // evaluates to true
    bool susanIsIncluded = (names & Names.Susan) != Names.None;
    
    // evaluates to false
    bool karenIsIncluded = (names & Names.Karen) != Names.None;
    

    Logical bitwise combinations can be tough to remember, so I make life easier on myself with a FlagsHelper class*:

    // The casts to object in the below code are an unfortunate necessity due to
    // C#'s restriction against a where T : Enum constraint. (There are ways around
    // this, but they're outside the scope of this simple illustration.)
    public static class FlagsHelper
    {
        public static bool IsSet<T>(T flags, T flag) where T : struct
        {
            int flagsValue = (int)(object)flags;
            int flagValue = (int)(object)flag;
    
            return (flagsValue & flagValue) != 0;
        }
    
        public static void Set<T>(ref T flags, T flag) where T : struct
        {
            int flagsValue = (int)(object)flags;
            int flagValue = (int)(object)flag;
    
            flags = (T)(object)(flagsValue | flagValue);
        }
    
        public static void Unset<T>(ref T flags, T flag) where T : struct
        {
            int flagsValue = (int)(object)flags;
            int flagValue = (int)(object)flag;
    
            flags = (T)(object)(flagsValue & (~flagValue));
        }
    }
    

    This would allow me to rewrite the above code as:

    Names names = Names.Susan | Names.Bob;
    
    bool susanIsIncluded = FlagsHelper.IsSet(names, Names.Susan);
    
    bool karenIsIncluded = FlagsHelper.IsSet(names, Names.Karen);
    

    Note I could also add Karen to the set by doing this:

    FlagsHelper.Set(ref names, Names.Karen);
    

    And I could remove Susan in a similar way:

    FlagsHelper.Unset(ref names, Names.Susan);
    

    *As Porges pointed out, an equivalent of the IsSet method above already exists in .NET 4.0: Enum.HasFlag. The Set and Unset methods don’t appear to have equivalents, though; so I’d still say this class has some merit.


    Note: Using enums is just the conventional way of tackling this problem. You can totally translate all of the above code to use ints instead and it’ll work just as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I have an async method: public async Task Do() { await Task.Delay(1000);
Let's say I have a method in java, which looks up a user in
Let's say I have five integer values that must all be unique. int a;
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have the following classes : public class MyProductCode { private String
Let's say you have a method that expects a numerical value as an argument.
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let say I have the following desire, to simplify the IConvertible's to allow me

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.