473,581 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitwise comparison and enumerations / switch statements

Hello!

Consider the following enumeration used to combine several of the values. I
am trying to figure out what the best solution is to check for the presence
of a value using bitwise comparisons. I'm able to get if statements to work,
but am unsure if switch statemens allow this.

Maybe it's just me on a friday, but if you've got a better solution than if
statements, please let me know.

[Flags()]
enum position
{
first = 1,
second = 2,
third = 4,
last = 8
}

...

// Create variable with two values from the enumeration
position p = position.first | position.second ;

switch (p)
{
case position.first: { .. }
case position.second : { .. }
}

// The following if statement works
if ((p & position.first) > 0) { .. }

// The following if statement also works
if ((p & position.first) == position.first) { .. }

Thanks in advance.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
3 12993
I'm able to get if statements to work,
but am unsure if switch statemens allow this.


switch only checks if the values are equal, so you'd need one case for
every combination of the flags.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hello Mattias

That's what I expected, but I wanted to ask anyway, just in case I had
missed something. I'll pass on the info to the team. If you have suggestions
for setting up the switch case expresion, please give it a go :-)

Thanks again.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #3
To determine if a given value is in another, do the following:

int total_val = 12;
int val_to_find = 4;

if( ((val_to_find & total_val) == val_to_find) )
//val_to_find is in the total_val
In short: ((val_to_find & total_val) == val_to_find)

I know this post is old...but wanted to reply anyway showing this short code.

Mark

From http://developmentnow.com/g/36_2004_...statements.htm

Posted via DevelopmentNow. com Groups
http://www.developmentnow.com
Nov 17 '05 #4

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

Similar topics

11
9005
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why anyone would use them - any real world examples or ideas? Examples follow (that I am reading in my Core JavaScript Guide 1.5). 15 & 9 yields 9 (1111...
21
1923
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method defined in foo.cpp needs to return typeNone. Is using static_cast< bar >( 0 )
12
18604
by: sandy_pt_in | last post by:
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible
6
8969
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 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the...
1
2891
by: Frank McCown | last post by:
I've created a quick reference comparison of C# and VB.NET that you might find useful. I couldn't find anything like it on the web and so I made my own. http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_compa rison.html It doesn't cover every area of the languages, just those features most frequently used.
8
6928
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 Keys.V element, regardless of any other keys. I know it will be a bitwise comparison, but I can't work out the correct syntax to use.
21
1933
by: Henry Padilla | last post by:
I'm writing a map generator and I'm looking for a way to keep track of the various terrain. I had a thought of using bitboards but I can't find a way to bitwise AND two int's together and get a bool. How do I get the proper response from: IF (0x01 & 0x05) My only recourse so far is to use the full if syntax: IF ((0x01 & 0x05) > 0)
2
2740
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 int column used for bitwise data - you know the sort of thing... 0, 1, 2, 4, 8, 16 etc intBitwise strName ----------------------
8
8781
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well. BRgds, Daniel.
0
7876
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...
0
7804
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...
0
8156
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. ...
1
5681
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...
0
5366
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...
0
3809
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...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
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
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.