Click to See Complete Forum and Search --> : Silly question


nemok
February 19th, 2006, 02:02 PM
Hi,

Let's say I have this code:

#define ITEM1 0x00000001
#define ITEM2 0x00000002
#define ITEM3 0x00000003
int x=0;
x|=ITEM1;
x|=ITEM2;
x|=ITEM3;

I know I can check if an item is added by using "&", but how can I remove one item from the variable?

googler
February 19th, 2006, 02:06 PM
This is not right first of all since when using bitmasks you should use non-overlapping bits.
#define ITEM1 0x00000001
#define ITEM2 0x00000002
#define ITEM3 0x00000004
int x=ITEM1 | ITEM2 | ITEM3;

but how can I remove one item from the variable?
Like this
x &= ~ITEM2;

JMS
February 19th, 2006, 02:36 PM
I know I can check if an item is added by using "&",




#define ITEM1 0x00000001
#define ITEM2 0x00000002
#define ITEM3 0x00000004
int x=ITEM1 | ITEM2 | ITEM3;

if( x & ITEM1 )
{
// Item is set
}
else
{
// Item is not set
}

Siddhartha
February 19th, 2006, 03:04 PM
I know I can check if an item is added by using "&", but how can I remove one item from the variable?Using bitwise XOR (if the item exists).

Like this -
if (x & ITEM1)
x ^= ITEM1;