CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Silly question

  1. #1
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    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?
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  2. #2
    Join Date
    Dec 2005
    Posts
    642

    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
    Code:
    x &= ~ITEM2;

  3. #3
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    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
    }

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Silly question

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured