Working with flags..

  • Thread starter Thread starter Ignacio X. Domínguez
  • Start date Start date
I

Ignacio X. Domínguez

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.
 
Ignacio said:
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/
 
Thanks. Yes, actually I'm using an enum, but to simplify the post I ommitted
that part. Thank you very much.


Jochen Kalmbach said:
Ignacio said:
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/
 
Back
Top