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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:39:33+00:00 2026-05-29T08:39:33+00:00

I have three enums, which – in combination – should identify a unique state

  • 0

I have three enums, which – in combination – should identify a unique state of affairs.

A struct of Enums, some bit fields, others not

enum KeyCode
{
    A,
    B,
    C,
    D,
    E
}

[Flags]
enum KeyModifiers
{
    Empty = 0,
    NoModifiers = 10000,
    Shift = 20000,
    Control = 40000,
    Alt = 80000
}

enum KeyState
{
    Empty = 0,
    Down = 10000000,
    Up = 20000000
}

I would like to implement a class which encompasses that state, and which would provide a Contains method, which would allow me to somehow treat the combination of these enums as a single value supporting bitwise operations.

But the current implementation does not work, I believe because some of the underlying enums are not bit fields.

Contains does not work due to KeyCode

class KeyEvent
{

    public KeyEvent(KeyCode key, KeyState down = KeyState.Empty, KeyModifiers modifiers = KeyModifiers.Empty)
    {
        Key = key;
        State = down;
        Modifiers = modifiers;
    }

    public int Id
    {
        get
        {
            return (int)Key + (int)State + (int)Modifiers;
        }
    }

    public KeyCode Key { get; set; }

    public KeyState State { get; set; }

    public KeyModifiers Modifiers { get; set; }

    public bool Contains(KeyEvent comparable)
    {
        return (Id & comparable.Id) == comparable.Id;
    }

}

How could I achieve my aim, which is to be able to compare the current values of those enums (this KeyEvent) with an input set of those enum values (as another KeyEvent) so as to determine whether the input values are contained within the current instance (but not necessarily equal to it) ?

  • I could simply compare the enums separately, but is there a better solution?
  • I do not want to create a fourth enum that combines all the other three in a bitwise manner, as KeyData does for KeyCode and modifiers
  • 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-29T08:39:34+00:00Added an answer on May 29, 2026 at 8:39 am

    You would have to either define KeyCode like this

    [Flags]
    enum KeyCode {
        A = 1,
        B = 2,
        C = 4,
        D = 8,
        E = 16
    } 
    

    Or to calculate the combined value with

    return (int)Math.Pow(2, (int)Key) + (int)State + (int)Modifiers;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data object which has a base class with three derived classes,
I am writing a small library with which enums in C++ should get easier.
Say I have an class which has three states: full screen, windowed, and minimized.
I have a MySQL Left Join problem. I have three tables which I'm trying
I have a class which constructor takes a Jakarta enums . I'm trying to
I have C headers which forward declare enums. Apparently, this is not possible in
can anyone help me with bit of refactoring advice. I have my App which
I have the three following enums: public enum SensorTypeA{A1,A2,...,A12}; public enum SensorTypeB{B1,B2,...,B12}; public enum
I'm using gcc, which implements enums as 32 bit integers on the architecture I
Is there a way to have a 64 bit enum in C++? Whilst refactoring

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.