bitwise OR AND, etc.

Discussion in 'C++' started by Travis, Sep 14, 2007.

  1. Travis

    Travis Guest

    I don't have too much experience with bitwise operations. Here is what
    I'm trying to accomplish. I have enum say:

    enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }

    Then I have an int that represents some combination of that enum,
    like:

    int WhoIsAllowed = BILL | KATIE | JOHN;

    Then I wanted to be able to check an instance against the
    WhoIsAllowed. like:

    int KnockingAtDoor = BILL;

    How can I check to see if BILL is in fact on the WhoIsAllowed?

    I thought something like

    if (KnockingAtDoor & WhoIsAllowed) { // let him in }

    but it doesn't seem to be working.

    Thanks.
    Travis, Sep 14, 2007
    #1
    1. Advertising

  2. Travis

    Ian Collins Guest

    Travis wrote:
    > I don't have too much experience with bitwise operations. Here is what
    > I'm trying to accomplish. I have enum say:
    >
    > enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }
    >
    > Then I have an int that represents some combination of that enum,
    > like:
    >
    > int WhoIsAllowed = BILL | KATIE | JOHN;
    >
    > Then I wanted to be able to check an instance against the
    > WhoIsAllowed. like:
    >
    > int KnockingAtDoor = BILL;
    >
    > How can I check to see if BILL is in fact on the WhoIsAllowed?
    >

    Change the enum so BILL has one bit set.

    --
    Ian Collins.
    Ian Collins, Sep 14, 2007
    #2
    1. Advertising

  3. Travis

    Travis Guest

    On Sep 14, 3:43 pm, Ian Collins <> wrote:
    > Travis wrote:
    > > I don't have too much experience with bitwise operations. Here is what
    > > I'm trying to accomplish. I have enum say:

    >
    > > enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }

    >
    > > Then I have an int that represents some combination of that enum,
    > > like:

    >
    > > int WhoIsAllowed = BILL | KATIE | JOHN;

    >
    > > Then I wanted to be able to check an instance against the
    > > WhoIsAllowed. like:

    >
    > > int KnockingAtDoor = BILL;

    >
    > > How can I check to see if BILL is in fact on the WhoIsAllowed?

    >
    > Change the enum so BILL has one bit set.
    >
    > --
    > Ian Collins.


    I'm sorry I'm not sure what you mean?
    Travis, Sep 14, 2007
    #3
  4. Travis

    Travis Guest

    On Sep 14, 3:47 pm, Travis <> wrote:
    > On Sep 14, 3:43 pm, Ian Collins <> wrote:
    >
    >
    >
    > > Travis wrote:
    > > > I don't have too much experience with bitwise operations. Here is what
    > > > I'm trying to accomplish. I have enum say:

    >
    > > > enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }

    >
    > > > Then I have an int that represents some combination of that enum,
    > > > like:

    >
    > > > int WhoIsAllowed = BILL | KATIE | JOHN;

    >
    > > > Then I wanted to be able to check an instance against the
    > > > WhoIsAllowed. like:

    >
    > > > int KnockingAtDoor = BILL;

    >
    > > > How can I check to see if BILL is in fact on the WhoIsAllowed?

    >
    > > Change the enum so BILL has one bit set.

    >
    > > --
    > > Ian Collins.

    >
    > I'm sorry I'm not sure what you mean?


    As in don't start with 0 so that every value has at least some bit
    set? I'll try that. Thanks.
    Travis, Sep 14, 2007
    #4
  5. Travis

    Travis Guest

    On Sep 14, 3:52 pm, Travis <> wrote:
    > On Sep 14, 3:47 pm, Travis <> wrote:
    >
    >
    >
    > > On Sep 14, 3:43 pm, Ian Collins <> wrote:

    >
    > > > Travis wrote:
    > > > > I don't have too much experience with bitwise operations. Here is what
    > > > > I'm trying to accomplish. I have enum say:

    >
    > > > > enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }

    >
    > > > > Then I have an int that represents some combination of that enum,
    > > > > like:

    >
    > > > > int WhoIsAllowed = BILL | KATIE | JOHN;

    >
    > > > > Then I wanted to be able to check an instance against the
    > > > > WhoIsAllowed. like:

    >
    > > > > int KnockingAtDoor = BILL;

    >
    > > > > How can I check to see if BILL is in fact on the WhoIsAllowed?

    >
    > > > Change the enum so BILL has one bit set.

    >
    > > > --
    > > > Ian Collins.

    >
    > > I'm sorry I'm not sure what you mean?

    >
    > As in don't start with 0 so that every value has at least some bit
    > set? I'll try that. Thanks.


    This brings up another question, is it possible to search for the
    absence of someone. Like

    pseudo
    if Zack ISNOTIN WhoIsAllowed
    {
    Add zack to list
    }
    Travis, Sep 14, 2007
    #5
  6. Travis wrote:
    > I don't have too much experience with bitwise operations. Here is what
    > I'm trying to accomplish. I have enum say:
    >
    > enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }


    write this like so:

    enum myEnum { BILL = 1<<0, KATIE = 1<<1, JOHN = 1<<2 };

    >
    > Then I have an int that represents some combination of that enum,
    > like:
    >
    > int WhoIsAllowed = BILL | KATIE | JOHN;
    >
    > Then I wanted to be able to check an instance against the
    > WhoIsAllowed. like:
    >
    > int KnockingAtDoor = BILL;
    >
    > How can I check to see if BILL is in fact on the WhoIsAllowed?
    >
    > I thought something like
    >
    > if (KnockingAtDoor & WhoIsAllowed) { // let him in }
    >
    > but it doesn't seem to be working.
    >
    > Thanks.
    >
    Gianni Mariani, Sep 15, 2007
    #6
  7. Travis

    Ian Collins Guest

    Travis wrote:

    Please trim your quotes!

    >
    > This brings up another question, is it possible to search for the
    > absence of someone. Like
    >
    > pseudo
    > if Zack ISNOTIN WhoIsAllowed
    > {
    > Add zack to list
    > }
    >

    Yes, use bitwise and.

    --
    Ian Collins.
    Ian Collins, Sep 15, 2007
    #7
  8. Travis

    Jim Langston Guest

    "Travis" <> wrote in message
    news:...
    > On Sep 14, 3:52 pm, Travis <> wrote:
    >> On Sep 14, 3:47 pm, Travis <> wrote:
    >>
    >>
    >>
    >> > On Sep 14, 3:43 pm, Ian Collins <> wrote:

    >>
    >> > > Travis wrote:
    >> > > > I don't have too much experience with bitwise operations. Here is
    >> > > > what
    >> > > > I'm trying to accomplish. I have enum say:

    >>
    >> > > > enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 }

    >>
    >> > > > Then I have an int that represents some combination of that enum,
    >> > > > like:

    >>
    >> > > > int WhoIsAllowed = BILL | KATIE | JOHN;

    >>
    >> > > > Then I wanted to be able to check an instance against the
    >> > > > WhoIsAllowed. like:

    >>
    >> > > > int KnockingAtDoor = BILL;

    >>
    >> > > > How can I check to see if BILL is in fact on the WhoIsAllowed?

    >>
    >> > > Change the enum so BILL has one bit set.

    >>
    >> > > --
    >> > > Ian Collins.

    >>
    >> > I'm sorry I'm not sure what you mean?

    >>
    >> As in don't start with 0 so that every value has at least some bit
    >> set? I'll try that. Thanks.

    >
    > This brings up another question, is it possible to search for the
    > absence of someone. Like
    >
    > pseudo
    > if Zack ISNOTIN WhoIsAllowed
    > {
    > Add zack to list
    > }


    Lets say Joe is 1 (1<<0)
    Lets say Zack is 2 (1<<1)
    Lets say Bill is 4 (1<<2)

    Lets say WhoIsAllowed is
    int WhoIsAllowed = Joe | Bill;

    Now, you want to know if Zack is allowed.

    if (WhoIsAllowed & Zack )
    // allowed
    else
    // Not allowed

    WhoIsAllowed & Zack will be equal to 0. 0 used as bool is false.
    WhoIsAllowed & Joe will be equal to 1. Non zeros used as bool is true.
    WhoIsAllowed & Bill will be equal to 4. Non zeros used as bool is true.

    No make it easier to figure out what you are doing, I would probalby code
    it:
    if ( WhoIsAllowed & Zack == 0 )
    // Not Allowed

    But again, read my prevoius response about why Iwouldn't use enums for htis
    typeof thing anyway and code that I would use.
    Jim Langston, Sep 15, 2007
    #8
  9. Travis

    Travis Guest

    Thanks but this way of smacking readable labels with enums makes much
    more sense. I don't have a ton users it's more like GOD, ADMIN, REG
    and that's it.

    Thank you everyone for your help.
    Travis, Sep 15, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robin Cull
    Replies:
    5
    Views:
    391
    Andrew Dalke
    Jul 31, 2003
  2. Daniel Joyce

    Python Audio (Alpy, Fastaudio, Etc Etc)

    Daniel Joyce, Sep 16, 2003, in forum: Python
    Replies:
    1
    Views:
    904
    Markus Wankus
    Sep 16, 2003
  3. Marco Herrn
    Replies:
    7
    Views:
    621
    Ng Pheng Siong
    Apr 9, 2004
  4. mdh

    Bitwise AND etc

    mdh, May 10, 2006, in forum: C Programming
    Replies:
    17
    Views:
    961
    Peter Shaggy Haywood
    May 14, 2006
  5. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    368
    Fredrik Lundh
    Aug 13, 2008
Loading...

Share This Page