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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:14:15+00:00 2026-05-12T19:14:15+00:00

I’m not very used to programming with flags, but I think I just found

  • 0

I’m not very used to programming with flags, but I think I just found a situation where they’d be useful:

I’ve got a couple of objects that register themselves as listeners to certain events. What events they register for is dependent on a variable that is sent to them when they are constructed. I think a nice way to do this would be to send bitwise OR connected variables, like such: TAKES_DAMAGE | GRABBABLE | LIQUID, etc. Then, in the constructor, the object can check what flags are set and register it self as listener for the ones that are.

But this is where I get confused. Preferably, the flags would be in an enum. But that is also a problem. If we have got these flags:

enum
{
    TAKES_DAMAGE,/* (0) */
    GRABBABLE, /* (1) */
    LIQUID, /* (2) */
    SOME_OTHER /* (3) */
};

Then sending the flag SOME_OTHER (3) will be the same as sending GRABBABLE | LIQUID, will it not?

How exactly do you deal with this stuff?

  • 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-12T19:14:16+00:00Added an answer on May 12, 2026 at 7:14 pm

    Your enumeration needs to be powers of two :

    enum
    {
        TAKES_DAMAGE = 1,
        GRABBABLE = 2,
        LIQUID = 4,
        SOME_OTHER = 8
    };
    

    Or in a more readable fashion :

    enum
    {
        TAKES_DAMAGE = 1 << 0,
        GRABBABLE = 1 << 1,
        LIQUID = 1 << 2,
        SOME_OTHER = 1 << 3
    };
    

    Why ? Because you want to be able to combine flags with no overlapping, and also be able to do this:

    if(myVar & GRABBABLE)
    {
        // grabbable code
    }
    

    … Which works if the enumeration values look like this :

     TAKES_DAMAGE: 00000001
     GRABBABLE:    00000010
     LIQUID:       00000100
     SOME_OTHER:   00001000
    

    So, say you’ve set myVar to GRABBABLE | TAKES_DAMAGE, here’s how it works when you need to check for the GRABBABLE flag:

     myVar:     00000011
     GRABBABLE: 00000010 [AND]
     -------------------
                00000010 // non-zero => converts to true
    

    If you’d set myVar to LIQUID | SOME_OTHER, the operation would have resulted in :

     myVar:     00001100
     GRABBABLE: 00000010 [AND]
     -------------------
                00000000 // zero => converts to false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.