|
-
February 19th, 2006, 03:02 PM
#1
Silly question
Hi,
Let's say I have this code:
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?
-
February 19th, 2006, 03:06 PM
#2
Re: Silly question
This is not right first of all since when using bitmasks you should use non-overlapping bits.
Code:
#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
-
February 19th, 2006, 03:36 PM
#3
Re: Silly question
I know I can check if an item is added by using "&",
Code:
#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
}
-
February 19th, 2006, 04:04 PM
#4
Re: Silly question
 Originally Posted by nemok
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 -
Code:
if (x & ITEM1)
x ^= ITEM1;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|