Thread: obtaining which combination of flags are being set

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    obtaining which combination of flags are being set

    I'm asking this question in order to clarify on how to process the flags. For example, if

    Code:
    FLONG flagSet = FO_DBCS_FONT | FO_GRAY16 // I'm trying to illustrate that both these flags are set
    in order to process flagSet to see which flags are set, would this approach be the standard one to go about it:
    Code:
    // determine whether FO_DBCS_FONT is set
    if( flagSet & FO_DBCS_FONT)
    {
    // process some code here
    }
    I may be completely thinking in the wrong tangent. Basically I'm trying to AND the two flags logically and see if I get non-zeros to determine whether a particular flag is set. Would this work in C++?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You line of thinking is correct.
    | to combine flags.
    & to test a flag.
    & ~() to remove a flag.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Thanks for the confirmation Elysia. Just one more question: in what situation would the developer want to remove a particular flag? and can you show me an example of how one would remove a flag?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int x = myflag1 | myflag2;
    x &= ~(myflag1);

    This removes flag 1.
    As for why... hmm, well, there are always those opportunities when you might want to.
    Say you might want to strip out flags depending on some other flag.
    Or you add a certain number of flags as default and in certain circumstances you want to remove a flag.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are plenty of reasons why you may want to remove a flag - a typical case would be that you have a flag to indicate if something is in progress or finished. If the flag is called "INPROGRESS", when we finish, we'd do something like this:
    Code:
    void finish(sometype *p)
    {
        p->flags &= ~INPROGRESS;
    }
    Other examples of "clearing" bits - whether they are flags or otherwise, would be when programming hardware registers, where you may want to change only certain bits in a hardware register.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM