473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create 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 12989
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
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...
21
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...
12
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
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...
1
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. ...
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...
21
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...
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...
8
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....
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
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
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
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...
1
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
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.