473,469 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.

Sep 14 '07 #1
8 1573
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.
Sep 14 '07 #2
On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.comwrote:
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?

Sep 14 '07 #3
On Sep 14, 3:47 pm, Travis <travis.bow...@gmail.comwrote:
On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.

Sep 14 '07 #4
On Sep 14, 3:52 pm, Travis <travis.bow...@gmail.comwrote:
On Sep 14, 3:47 pm, Travis <travis.bow...@gmail.comwrote:
On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.comwrote:
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
}

Sep 14 '07 #5
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.
Sep 14 '07 #6
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.
Sep 14 '07 #7
"Travis" <tr***********@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 14, 3:52 pm, Travis <travis.bow...@gmail.comwrote:
>On Sep 14, 3:47 pm, Travis <travis.bow...@gmail.comwrote:
On Sep 14, 3:43 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.
Sep 14 '07 #8
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.

Sep 15 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.