Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   bitwise OR AND, etc. (http://www.velocityreviews.com/forums/t537376-bitwise-or-and-etc.html)

 Travis 09-14-2007 10:39 PM

bitwise OR AND, etc.

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.

 Ian Collins 09-14-2007 10:43 PM

Re: bitwise OR AND, etc.

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.

 Travis 09-14-2007 10:47 PM

Re: bitwise OR AND, etc.

On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.com> 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 09-14-2007 10:52 PM

Re: bitwise OR AND, etc.

On Sep 14, 3:47 pm, Travis <travis.bow...@gmail.com> wrote:
> On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.com> 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 09-14-2007 10:55 PM

Re: bitwise OR AND, etc.

On Sep 14, 3:52 pm, Travis <travis.bow...@gmail.com> wrote:
> On Sep 14, 3:47 pm, Travis <travis.bow...@gmail.com> wrote:
>
>
>
> > On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.com> 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
{
}

 Gianni Mariani 09-14-2007 11:01 PM

Re: bitwise OR AND, etc.

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.
>

 Ian Collins 09-14-2007 11:21 PM

Re: bitwise OR AND, etc.

Travis wrote:

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

Yes, use bitwise and.

--
Ian Collins.

 Jim Langston 09-14-2007 11:24 PM

Re: bitwise OR AND, etc.

"Travis" <travis.bowers@gmail.com> wrote in message
> On Sep 14, 3:52 pm, Travis <travis.bow...@gmail.com> wrote:
>> On Sep 14, 3:47 pm, Travis <travis.bow...@gmail.com> wrote:
>>
>>
>>
>> > On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.com> 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
> {
> }

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.

 Travis 09-15-2007 12:02 AM

Re: bitwise OR AND, etc.

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.

 All times are GMT. The time now is 12:01 PM.