Thread: Examining flags when passed to a function?

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Question Examining flags when passed to a function?

    The syntax of my question is likely to be totally incorrect! This is what I meant:

    When a function recieves a single variable that contains a set of flags seperated by the bitwise operator, how does it read those values and how are those values defined? i.e. I would like to write my own function that will take a set of flags as an argument.

    e.g.
    // Declaration
    int Function(DWORD flags);

    // Usage
    i = Function(FLAG_X | FLAG_Y | FLAG_Z);

    // Function Code:
    ?????????!

    Hope this makes sense, and thanks in advance

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is one solution.

    Code:
    enum TypeX {tX1 = 0x0001, tX2 = 0x02,  tX3 = 0x0003}
    
    void DetermineType(const int nType)
    {	
       for(int type = tX1; type <= tX3; type = (type << 1))
      {
          if (nType & type)
             // Type match.  Do something.
       }
    }
    Kuphryn

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Umm kuphryn, you cannot have 3 as a flag.
    A flag must be a single bit, therefore valid flags are powers of 2.
    1
    2
    4
    8
    16
    32
    ... and so on.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    let's say you want to use a 32 bit (4 byte ) integer to hold some flags. This just means to lay out the integer in binary format and and use each individual bit as an on/off, true/false, or similar binary flag.


    00000000 00000000 00000000 00000000

    Above is an integer with all bits 'cleared'.

    01000000 00000000 00000000 00000000

    Now we have set the second bit. To set a bit, we can | (OR) it to the number. We can do this in two ways. First, we can choose the zero-based index of the bit:


    #define TRUE 1
    int offset = 1; // second bit position

    flag |= (TRUE << offset);

    Would result in the second bit being set.

    Second, you can simply use an integer value:

    int value = 2;

    flag |= value;

    To determine if a bit is set:

    if(flag & (TRUE << offset))
    // then bit is set

    or:

    if(flag & value)
    // then bit is set

    To clear a bit & it with it's bit complement (~):

    flag &= ~(TRUE << offset);

    or:

    flag &= ~value;


    Hope that helps.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Correct. Modify the enum accordingly.

    Kuphryn

  6. #6
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Brilliant, thanks guys, I think I get it now :-)

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM