Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bitwise OR AND, etc.

Reply
Thread Tools

bitwise OR AND, etc.

 
 
Travis
Guest
Posts: n/a
 
      09-14-2007
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.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-14-2007
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.
 
Reply With Quote
 
 
 
 
Travis
Guest
Posts: n/a
 
      09-14-2007
On Sep 14, 3:43 pm, Ian Collins <(E-Mail Removed)> 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?

 
Reply With Quote
 
Travis
Guest
Posts: n/a
 
      09-14-2007
On Sep 14, 3:47 pm, Travis <(E-Mail Removed)> wrote:
> On Sep 14, 3:43 pm, Ian Collins <(E-Mail Removed)> 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.

 
Reply With Quote
 
Travis
Guest
Posts: n/a
 
      09-14-2007
On Sep 14, 3:52 pm, Travis <(E-Mail Removed)> wrote:
> On Sep 14, 3:47 pm, Travis <(E-Mail Removed)> wrote:
>
>
>
> > On Sep 14, 3:43 pm, Ian Collins <(E-Mail Removed)> 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
}

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      09-14-2007
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.
>

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-14-2007
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.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-14-2007
"Travis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Sep 14, 3:52 pm, Travis <(E-Mail Removed)> wrote:
>> On Sep 14, 3:47 pm, Travis <(E-Mail Removed)> wrote:
>>
>>
>>
>> > On Sep 14, 3:43 pm, Ian Collins <(E-Mail Removed)> 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.


 
Reply With Quote
 
Travis
Guest
Posts: n/a
 
      09-15-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BitWise Operations =?Utf-8?B?Sm9u?= ASP .Net 3 01-24-2006 09:19 AM
bitwise comparator Random ASP .Net 2 06-08-2005 05:28 PM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-21-2003 03:55 AM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 1 10-21-2003 03:46 AM
Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-20-2003 09:26 PM



Advertisments