Thread: [win32] - how use bit flags?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    [win32] - how use bit flags?

    using '&', '|','!' and others with bits, the manual tells us how. but not how compare them
    see these consts:

    Code:
    const int RANDOM=1;
    const int IMMEDIATE=2;
    const int SEARCH=3;
    
    int a;
    imagine that 'a' have RANDOM and IMMEDIATE. how can i test it?
    (maybe these consts values aren't correct, but these is for i understand how can i compare several values with bit flags)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    First of all, SEARCH is 3, which means it's a combination of RANDOM and IMMEDIATE -- SEARCH == RANDOM | IMMEDIATE. Is that intended?

    To test if a flag is set, just use (variable & FLAG):

    Code:
    if (flags & RANDOM)
    {
        // it's random
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Also, you have to test for each bit individually - (a & RANDOM) or (a & IMMEDIATE)
    So you could not use (a & SEARCH). It would be TRUE if any or all of those bits were set.

    To test for SEARCH, you could use (a & SEARCH == SEARCH).

    Some predefined flags in the windows API are combinations of other flags.

    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 Console Application event handlers, or Win32 Project?
    By Joewbarber in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2013, 07:02 AM
  2. flags on gcc
    By alex83 in forum C Programming
    Replies: 2
    Last Post: 12-04-2010, 08:58 AM
  3. Using 'flags'
    By cyreon in forum C++ Programming
    Replies: 11
    Last Post: 01-28-2008, 04:21 PM
  4. set flags
    By glue21 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 01:15 PM
  5. What are Flags?
    By Blanket in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2003, 08:04 AM