[win32] - how use bit flags?

This is a discussion on [win32] - how use bit flags? within the Windows Programming forums, part of the Platform Specific Boards category; using '&', '|','!' and others with bits, the manual tells us how. but not how compare them see these consts: ...

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

    [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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    500
    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, 08: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, 02:15 PM
  5. What are Flags?
    By Blanket in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2003, 08:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21