473,663 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on Enum flags

I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventTyp e dEventType;

if(DisputesChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionCheckB ox.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an action
or any combination of the 3

how do I do that?


BillG
Jun 27 '08 #1
6 1354
"BillG" <bi*******@char ter.netwrote in message
news:%2******** *******@TK2MSFT NGP02.phx.gbl.. .
>I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventTyp e dEventType;

if(DisputesChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionCheckB ox.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?


BillG

//
if ((dEventType & DisplayEventTyp e.ACTION) == DisplayEventTyp e.ACTION)
DoSomething();
//

From wikipedia (http://en.wikipedia.org/wiki/Bitwise_operation):

"A bitwise AND takes two binary representations of equal length and performs
the logical AND operation on each pair of corresponding bits. In each pair,
the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise,
the result is 0. For example:

0101
AND 0011
= 0001
"

Basically, if the dEventType has all three values set, and you AND it with
one of those values (ACTION), it will return the value with which you AND it
(ACTION). All bits will return 0 except for the ACTION bit, because only
the ACTION bit is represented in both dEventType and
DisplayEventTyp e.ACTION.

Jun 27 '08 #2
LVP
Use OR not And might help you,

enum DisplayEventTyp e { WALASHE = 0x0000, DISPUTE = 0x0001, ACTION = 0x0002,
QUESTION = 0x0004 };
DisplayEventTyp e dEventType= DisplayEventTyp e.WALASHE; // Initialize it.

if(DisputesChec kBox.Checked)
dEventType = dEventType | DisplayEventTyp e.DISPUTE;
if(ActionCheckB ox.Checked)
dEventType = dEventType | DisplayEventTyp e.ACTION;
if(QuestionChec kBox.Checked)
dEventType = dEventType | DisplayEventTyp e.QUESTION;
LVP
"BillG" <bi*******@char ter.netwrote in message
news:#Z******** *****@TK2MSFTNG P02.phx.gbl...
I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventTyp e dEventType;

if(DisputesChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionCheckB ox.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?


BillG

Jun 27 '08 #3
but what if I want to know if both disputes and questions are in there or
even all three because I want to filter records depending on whether one or
more or all the boxes are checked.

BillG
"Nathan" <ms********@mac gregorfamily.ne twrote in message
news:09******** *************** ***********@mic rosoft.com...
"BillG" <bi*******@char ter.netwrote in message
news:%2******** *******@TK2MSFT NGP02.phx.gbl.. .
>>I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventTy pe dEventType;

if(DisputesChe ckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionCheck Box.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionChe ckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?


BillG


//
if ((dEventType & DisplayEventTyp e.ACTION) == DisplayEventTyp e.ACTION)
DoSomething();
//

From wikipedia (http://en.wikipedia.org/wiki/Bitwise_operation):

"A bitwise AND takes two binary representations of equal length and
performs the logical AND operation on each pair of corresponding bits. In
each pair, the result is 1 if the first bit is 1 AND the second bit is 1.
Otherwise, the result is 0. For example:

0101
AND 0011
= 0001
"

Basically, if the dEventType has all three values set, and you AND it with
one of those values (ACTION), it will return the value with which you AND
it (ACTION). All bits will return 0 except for the ACTION bit, because
only the ACTION bit is represented in both dEventType and
DisplayEventTyp e.ACTION.

Jun 27 '08 #4
Sorry that was a type, in my source code I do have | instead of &.

Bill

"LVP" <sq**********@h otmail.comwrote in message
news:42******** *************** ***********@mic rosoft.com...
Use OR not And might help you,

enum DisplayEventTyp e { WALASHE = 0x0000, DISPUTE = 0x0001, ACTION =
0x0002, QUESTION = 0x0004 };
DisplayEventTyp e dEventType= DisplayEventTyp e.WALASHE; // Initialize it.

if(DisputesChec kBox.Checked)
dEventType = dEventType | DisplayEventTyp e.DISPUTE;
if(ActionCheckB ox.Checked)
dEventType = dEventType | DisplayEventTyp e.ACTION;
if(QuestionChec kBox.Checked)
dEventType = dEventType | DisplayEventTyp e.QUESTION;
LVP
"BillG" <bi*******@char ter.netwrote in message
news:#Z******** *****@TK2MSFTNG P02.phx.gbl...
>I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventTy pe dEventType;

if(DisputesChe ckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionCheck Box.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionChe ckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?


BillG


Jun 27 '08 #5
I'm sure there are a number of ways to disect a bitwise enumeration, but I
don't claim to be an expert on bits. Depending on how your filtering is
done (is it direct SQL to a db, LINQ, dataview filter?), there might be a
clever way of setting it up.

"BillG" <bi*******@char ter.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
but what if I want to know if both disputes and questions are in there or
even all three because I want to filter records depending on whether one
or more or all the boxes are checked.

BillG
"Nathan" <ms********@mac gregorfamily.ne twrote in message
news:09******** *************** ***********@mic rosoft.com...
>"BillG" <bi*******@char ter.netwrote in message
news:%2******* ********@TK2MSF TNGP02.phx.gbl. ..
>>>I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventT ype dEventType;

if(DisputesCh eckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionCh eckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?


BillG


//
if ((dEventType & DisplayEventTyp e.ACTION) == DisplayEventTyp e.ACTION)
DoSomething();
//

From wikipedia (http://en.wikipedia.org/wiki/Bitwise_operation):

"A bitwise AND takes two binary representations of equal length and
performs the logical AND operation on each pair of corresponding bits. In
each pair, the result is 1 if the first bit is 1 AND the second bit is 1.
Otherwise, the result is 0. For example:

0101
AND 0011
= 0001
"

Basically, if the dEventType has all three values set, and you AND it
with one of those values (ACTION), it will return the value with which
you AND it (ACTION). All bits will return 0 except for the ACTION bit,
because only the ACTION bit is represented in both dEventType and
DisplayEventTy pe.ACTION.

Jun 27 '08 #6
BillG wrote:
but what if I want to know if both disputes and questions are in there or
even all three because I want to filter records depending on whether one or
more or all the boxes are checked.

BillG
"Nathan" <ms********@mac gregorfamily.ne twrote in message
news:09******** *************** ***********@mic rosoft.com...
>"BillG" <bi*******@char ter.netwrote in message
news:%2******* ********@TK2MSF TNGP02.phx.gbl. ..
>>I have the following defined.

enum DisplayEventTyp e{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventT ype dEventType;

if(DisputesCh eckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.DISPUTE;
if(ActionChec kBox.Checked)
dEventType = dEventType & DisplayEventTyp e.ACTION;
if(QuestionCh eckBox.Checked)
dEventType = dEventType & DisplayEventTyp e.QUESTION;

later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?


BillG

//
if ((dEventType & DisplayEventTyp e.ACTION) == DisplayEventTyp e.ACTION)
DoSomething();
//

From wikipedia (http://en.wikipedia.org/wiki/Bitwise_operation):

"A bitwise AND takes two binary representations of equal length and
performs the logical AND operation on each pair of corresponding bits. In
each pair, the result is 1 if the first bit is 1 AND the second bit is 1.
Otherwise, the result is 0. For example:

0101
AND 0011
= 0001
"

Basically, if the dEventType has all three values set, and you AND it with
one of those values (ACTION), it will return the value with which you AND
it (ACTION). All bits will return 0 except for the ACTION bit, because
only the ACTION bit is represented in both dEventType and
DisplayEventTy pe.ACTION.

To check for two types:
if ((dEventType & (DisplayEventTy pe.DISPUTE |
DisplayEventTyp e.QUESTION)) == DisplayEventTyp e.DISPUTE |
DisplayEventTyp e.QUESTION) { }

In a more readable way:

DisplayEventTyp e filter = DisplayEventTyp e.DISPUTE |
DisplayEventTyp e.Question;
if (dEventType & filter == filter) { }
--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #7

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

Similar topics

0
951
by: Ido Tamir | last post by:
Hi, I have an enum, defined as follows: public enum Flags { ON = 0, OFF = 1 } I want the following xml to be deserialized into that enum, without an
1
1603
by: | last post by:
Would be nice to specify an incremenet or decremenet as an attribute in an enum so I can for example have the following... enum SomeEnum { A = 0, B, // 2 C, // 4 D /6
21
4586
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
1
4162
by: todorov-fkt | last post by:
Hello everyone, I have a field which is 1 byte long and is used to store different flags (according to specs) - it is the flags bit in the ID3 tag header. So 0xabc00000 represents the byte, where only bits a,b and c have any meaning, the other bits are always 0. I also have the following: enum ID3_Header_Flags { NoFlag = 0x00000000, FlagC = 0x00100000, FlagB = 0x01000000,
1
10452
by: Dwight.Dexter | last post by:
I have an enum variable. Is there some way to cast an int or BitArray and get my enum value? I have to communicate with an old C-style sever so I get an array of bits("0","1"). But I'm using an enum variable to store the data. public enum LaneCapabilities { MLT = 0, ACM = 1,
4
6674
by: Paul E Collins | last post by:
The help file says that "bit fields can be combined using a bitwise OR operation, whereas enumerated constants cannot", and yet this code works: enum Blah { a, b, c }; // without .... Blah x = Blah.a | Blah.b; So what is the purpose of the Flags attribute? I understand it's potentially useful for reflection, so that (for example) the IDE's IntelliSense knows which enums are meant to be combined, but does it
2
2208
by: Ray Cassick \(Home\) | last post by:
I have a function that takes a value is as System.Enum and I want to be able to look at that value and determine if it is a regular enum or an enum that has a <Flag()> attribute set on it. I am trying to write a wrapper function that takes an enum and an integer value and validates if the value is actually part of the enum. Just trying to use .IsDefined will not return accurate results (as far as I can find) if the enum is a flag so if...
2
7407
by: Jigar.Patel | last post by:
Hi, I have following enum definition. public enum OperationTypes { All = 1, New = 2, View = 4,
7
1797
by: csharpula csharp | last post by:
Hello, I have methods which refer to some class enumerator .Should I make it pulic or encapsulate it? *** Sent via Developersdex http://www.developersdex.com ***
13
2452
by: At_sea_with_C | last post by:
Consider the following code: #include <stdio.h> #define SOMEVAL 1234 enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW }; void print_val(enum tree_types tree) { printf("tree is: %d\n", tree);
0
8436
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4182
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.