Options joined with |

This is a discussion on Options joined with | within the C++ Programming forums, part of the General Programming Boards category; I'm writing a class that I want to be able to accept several options with one parameter, similar to the ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Options joined with |

    I'm writing a class that I want to be able to accept several options with one parameter, similar to the way you can specify setiosflags( ios::fixed | ios::showpoint )

    I tried to check the type of some of these ios flags, but they are enum's, which doesn't help me very much. I understand that for int's, the | operator effectively adds them ( 5 | 2 = 7, for example ), and that by using ints with values that are powers of 2, one can figure out which combination of objects was passed by looking at the sum. Is that usually how it's done (using int's)? If so, is there an easy way to break the sum down into the individual combinations? The only way I can think of immediately is to divide by the highest power of 2 that leaves a power of 2 as a remainder, but it seems like it could be easier...TIA for any help.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The bitwise OR operator does not necessarily add the two numbers. It may work for 5 and 2 but try 3 and 1. You will still get 3. It just OR's the bits of the 2 numbers. You can look up the truth tables online somewhere.

    Try using each power of 2 as a flag. Like.

    Code:
    #define EFFECT_1  0x000F
    #define EFFECT_2  0x00F0
    #define EFFECT_3  0x0F00
    #define EFFECT_4  0xF000
    That way if you have an int or something full of effects, which you SET by
    Code:
    int effect |= EFFECT_1;
    The |= will keep any other effect you already had and add this one.

    To CHECK an effect do this.
    Code:
    if( effect & EFFECT_4 )
    {
      // Effect 4 is on
    }
    Hope this helps a little.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I understand that for int's, the | operator effectively adds them ( 5 | 2 = 7, for example )
    Not really. That only applies if both numbers have no set bits in common:

    5 | 2 = 101 | 010 = 111 = 7
    5 | 4 = 101 | 100 = 101 = 5


    Anyway, back to your question... which I didn't really get . It is true that flags are numbers written on the form 2^n (0 <= n <= MAXBITS), and that you can store several flags in one variable.

    To set one flag, use:
    FlagStorage |= Flag;

    To clear one flag, use:
    (MAXVALUE is the greatest value for the datatype of FlagStorage, ie 255 for a char)
    FlagStorage &= (MAXVALUE - Flag);

    To toggle one flag, use:
    FlagStorage ^= Flag;

    To retrieve one flag, use:
    bool IsItSet = (FlagStorage & Flag);


    EDIT:
    Damn, not just beaten once but beaten twice... ...
    Last edited by Magos; 04-16-2003 at 11:16 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    FlagStorage &= (MAXVALUE - Flag);
    cmon magos you can do better than that....

    dont forget there is a ~ operator.

    FlagStorage &= ~Flag
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Stoned_Coder
    cmon magos you can do better than that....

    dont forget there is a ~ operator.

    FlagStorage &= ~Flag
    Bah, ok then...
    You win this one
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Thanks, all, especially Salem and Wizard. That's exactly what I needed to know, and thanks for clearing up the bit about | and adding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 02:24 PM
  2. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 05:06 PM
  3. Options for a Server
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-10-2004, 10:10 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 08:06 AM

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