473,625 Members | 2,600 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.V alue1 | 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 1768
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*******@gmai l.comwrote in message
news:af******** *************** ***********@x35 g2000hsb.google groups.com...
Hello.

This question may be stupid.

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

...in the code...
MyValues test=MyValues.V alue1 | 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.V alue1 | 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.d kwrote:
Sin Jeong-hun wrote:
This question may be stupid.
enum MyValues
{
* Value1, Value2, Value3, Value4, Value5
}
...in the code...
MyValues test=MyValues.V alue1 | 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(M yValues.Value1) ) <== my imaginary Contains()
method
{
Do something
}
Jul 16 '08 #4
On Jul 15, 3:35*pm, Sin Jeong-hun <typing...@gmai l.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.d kwrote:
>Sin Jeong-hun wrote:
>>This question may be stupid.
enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}
...in the code...
MyValues test=MyValues.V alue1 | 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(M yValues.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.ToInt3 2(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.WriteLi ne((o & ABC.A) 0);
Console.WriteLi ne((o & ABC.B) 0);
Console.WriteLi ne((o & ABC.C) 0);
Console.WriteLi ne(o.Contains(A BC.A));
Console.WriteLi ne(o.Contains(A BC.B));
Console.WriteLi ne(o.Contains(A BC.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
2657
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 prompt user again.
14
2936
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 'null-terminated-string' to some functions, such as 'atoi'. (We assume that the string was extracted using an array, not string literal) This program shows blank for '\0' for all 3 cases. Why ? And I was trying to pass some strings formed using an array, without...
99
5125
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
4
18669
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 check if the page is valid before it gets posted back to the server so the window or box only pops up if everything is OK? At the moment the popup occurs first then the validation. Many thanks
20
2067
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 can I do it? Thanks
5
1344
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 to be compared to the text in the textbox (note: items in listbox are single
16
3513
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 instructions which can sure be optimized away is the check for the break condition, at least within the time where it is known that the loop will not reach it. Any idea how to write such a loop? e.g.
2
3741
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 to the main form is passed to its constructor. The connection class opens up a new thread and starts doing work in it, and adds collected data to the main form via a (cross-thread) control invoking delegate.
10
3233
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 capture this specific error? Would it make more sense to actually run a SELECT query first and if that returned a result, then I use that for error checking and don't run insert until select returns nothing? Cheers
0
8182
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
8688
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
8635
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...
1
8352
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
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...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
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...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
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 we have to send another system

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.