473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Working with flags..

Hi. I'm using the | operator to use multiple flags in a variable. For
example:

const int flag1 = 0x00000001;
const int flag2 = 0x00000002;
const int flag3 = 0x00000004;

int Flags = flag2 | flag4 | flag1;

Now I would like to remove flag2 from Flags without having to assign the
other flags (since I'm not sure which are enabled). I believe it's something
like

Flags = Flags (operator) flag2;

but i'm not sure about the operator.

Thanks in advance.
Nov 16 '05 #1
3 2529
Ignacio X. Domínguez wrote:
Hi. I'm using the | operator to use multiple flags in a variable. For
example:

const int flag1 = 0x00000001;
const int flag2 = 0x00000002;
const int flag3 = 0x00000004;

int Flags = flag2 | flag4 | flag1;

Now I would like to remove flag2 from Flags without having to assign
the other flags (since I'm not sure which are enabled). I believe it's
something like
Flags &= ~flag2;
Flags = Flags (operator) flag2;

Flags = Flags & (~flag2);
By the way: Why do you not use an enum !?

[Flags]
public enum PossibleFlags : int
{
flag1 = 0x1,
flag2 = 0x2,
flag3 = 0x4,
}

PossibleFlags Flags;
Flags = PossibleFlags.flag1 | PossibleFlags.flag2 | PossibleFlags.flag3;

Flags &= ~PossibleFlags.flag2;
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 16 '05 #2
Thanks. Yes, actually I'm using an enum, but to simplify the post I ommitted
that part. Thank you very much.
"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn**********************************@207.46.2 48.16...
Ignacio X. Domínguez wrote:
Hi. I'm using the | operator to use multiple flags in a variable. For
example:

const int flag1 = 0x00000001;
const int flag2 = 0x00000002;
const int flag3 = 0x00000004;

int Flags = flag2 | flag4 | flag1;

Now I would like to remove flag2 from Flags without having to assign
the other flags (since I'm not sure which are enabled). I believe it's
something like


Flags &= ~flag2;
Flags = Flags (operator) flag2;

Flags = Flags & (~flag2);
By the way: Why do you not use an enum !?

[Flags]
public enum PossibleFlags : int
{
flag1 = 0x1,
flag2 = 0x2,
flag3 = 0x4,
}

PossibleFlags Flags;
Flags = PossibleFlags.flag1 | PossibleFlags.flag2 | PossibleFlags.flag3;

Flags &= ~PossibleFlags.flag2;
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 16 '05 #3
XOR:

Flags ^= flag2;
Nov 16 '05 #4

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

Similar topics

3
by: Xah Lee | last post by:
Python re module has methods flags and pattern. How to use these exactly? e.g. i tried print patternObj.flags() and the error is some "int object is not callable". ...
7
by: loquak | last post by:
Greets. I wonder which way would be the best for holding some state flags in terms of memory use and efficiency. a) struct FLAGS { bool flag1 : 1; bool flag2 : 1;
6
by: Alexis Gatt | last post by:
Hi guys, I was reading the source code of a lib, and I came across this odd way of defining flags #define PHONG_RV (0<<0) #define PHONG_NH (1<<0) Here is how they're used:
13
by: Mark A. Odell | last post by:
I write a lot of drivers and there is inevitably some hardware register or buffer descriptor field called 'flags'. The flags are defined, typically, as bit positions. I was thinking I could get...
1
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,...
1
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...
1
by: RicercatoreSbadato | last post by:
I have notice that my images have the value Image.Flags = 2 (HasAlpha). How Can I set the Flags to 1? I would like to eliminate the alpha channel... -- RicercatoreSbadato
4
by: Joel Moore | last post by:
Say I have an enum similar to this: <Flags()> Enum TestFlags As Short One = 0 Two = 1 Three = 2 Four = 4 End Enum I figured I could declare variable of type TestFlags to be used in the
1
by: Alexander Korsunsky | last post by:
Hi! Is it possible to extract the mode flags of a stream (FILE* stream or fstream - object), without looking at how the stream was opened? What I mean by mode flags is wether the stream is opened...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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...
0
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,...

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.