473,499 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking if it contains a flag.

Hello.

This question may be stupid.

enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}

....in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;

....to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
Do something.
}

I always did that way but is that the cleanest way? Is there more
shorter expression for that?
Jul 15 '08 #1
5 1757
I have always found such expressions to look 'strange' for a simple
operation like this but as far as I know, there is no cleaner way to check
if a flag is present.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info

"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:af**********************************@x35g2000 hsb.googlegroups.com...
Hello.

This question may be stupid.

enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}

...in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;

...to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
Do something.
}

I always did that way but is that the cleanest way? Is there more
shorter expression for that?
Jul 15 '08 #2
Sin Jeong-hun wrote:
This question may be stupid.

enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}

...in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;

...to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
Do something.
}

I always did that way but is that the cleanest way? Is there more
shorter expression for that?
It is what MS prescribe:

http://msdn.microsoft.com/en-us/library/cc138362.aspx

I would probably have tested >0.

Arne

Jul 16 '08 #3
On Jul 16, 9:09*am, Arne Vajhøj <a...@vajhoej.dkwrote:
Sin Jeong-hun wrote:
This question may be stupid.
enum MyValues
{
* Value1, Value2, Value3, Value4, Value5
}
...in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;
...to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
* Do something.
}
I always did that way but is that the cleanest way? Is there more
shorter expression for that?

It is what MS prescribe:

http://msdn.microsoft.com/en-us/library/cc138362.aspx

I would probably have tested >0.

Arne

Thank you. I'd never looked that page before but if Microsoft have
explained that way, then maybe that's final. I just wondered if there
could be something like
if( test.Contains(MyValues.Value1) ) <== my imaginary Contains()
method
{
Do something
}
Jul 16 '08 #4
On Jul 15, 3:35*pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hello.
Hello.
I always did that way but is that the cleanest way? Is there more
shorter expression for that?
Yes, use bit values and then you can XOR, AND, OR, >>, << the bits.
But beware: professional programmers, of the kind that write chess
programs where speed is essential (you have to search a large part of
a chess tree of moves in five seconds or less) have complained that
this is tedious and error prone. One reason I stay away from even
using enum or bit values, etc at all. Just use integers and comment
your code so the reader knows what integer does what. But that's just
me, a newbie.

Hope this 'helps'.

RL

Jul 16 '08 #5
Sin Jeong-hun wrote:
On Jul 16, 9:09 am, Arne Vajhøj <a...@vajhoej.dkwrote:
>Sin Jeong-hun wrote:
>>This question may be stupid.
enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}
...in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;
...to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
Do something.
}
I always did that way but is that the cleanest way? Is there more
shorter expression for that?
It is what MS prescribe:

http://msdn.microsoft.com/en-us/library/cc138362.aspx

I would probably have tested >0.

Thank you. I'd never looked that page before but if Microsoft have
explained that way, then maybe that's final. I just wondered if there
could be something like
if( test.Contains(MyValues.Value1) ) <== my imaginary Contains()
method
{
Do something
}
If on .NET 3.5 you could write an extension method.

Se below for inspiration.

Arne

=================================================

using System;

namespace E
{
public static class MyExtensions
{
public static bool Contains(this Enum o, Enum v)
{
return (Convert.ToInt32(o) & Convert.ToInt32(v)) 0;
}
}
[Flags]
public enum ABC { A, B, C };
public class Program
{
public static void Main(string[] args)
{
ABC o = ABC.A | ABC.C;
Console.WriteLine((o & ABC.A) 0);
Console.WriteLine((o & ABC.B) 0);
Console.WriteLine((o & ABC.C) 0);
Console.WriteLine(o.Contains(ABC.A));
Console.WriteLine(o.Contains(ABC.B));
Console.WriteLine(o.Contains(ABC.C));
Console.ReadKey();
}
}
}
Jul 18 '08 #6

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

Similar topics

9
2641
by: Bart Nessux | last post by:
Are these equivelent? Is one approach prefered over the other #check to see if var contains something... if so proceed. if var is not None: continue #check to see if var is empty... if so...
14
2907
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
99
5037
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
4
18649
by: Emilio | last post by:
Hi, I have a form that contains various server validation controls, the submit button will either open a popup box or a popup window on the client-side click event. The question is, how do I...
20
2047
by: cozzzy | last post by:
Hello, I have an unsigned int variable, which is really a set of bits. So what I need, is to check specific bit state: on/off (0 or 1). For example, I need to know is the 16-th bit is on. How...
5
1334
by: Kendall | last post by:
Hi, My form is - a listbox which contains 5 items, a textbox and a button. ListBox - subjectListBox TextBox - subjectTextBox When the button is clicked, I want the 5 items in the listbox...
16
3481
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
3726
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference...
10
3217
by: Phil Latio | last post by:
I am inserting data into user table which contains 5 fields, sounds simple enough normally but 2 of the fields are designated as UNIQUE. If someone does enter a value which already exists, how do I...
0
7014
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...
0
7180
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,...
0
5485
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,...
1
4921
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...
0
3108
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
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
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...

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.