Which is the best way for holding flags?

Discussion in 'C++' started by loquak, Nov 30, 2004.

  1. loquak

    loquak Guest

    Greets.
    I wonder which way would be the best for holding some state flags in terms
    of memory use and efficiency.

    a)

    struct FLAGS
    {
    bool flag1 : 1;
    bool flag2 : 1;
    bool flag3 : 1;
    ....
    }
    FLAGS flags;

    b)

    enum FLAGS
    {
    flag1,
    flag2,
    flag3
    ....
    }
    std::bitset<FLAGS> flags;

    c)

    bool flag1;
    bool flag2;
    bool flag3;
    ....

    Which way would you prefer?
     
    loquak, Nov 30, 2004
    #1
    1. Advertising

  2. "loquak" <> wrote in message
    news:cogik6$u0s$...
    > Greets.
    > I wonder which way would be the best for holding some state flags in terms
    > of memory use and efficiency.
    >
    > a)
    >
    > struct FLAGS
    > {
    > bool flag1 : 1;
    > bool flag2 : 1;
    > bool flag3 : 1;
    > ...
    > }


    I hope it will not offend you if I say: Yuck.

    > FLAGS flags;
    >
    > b)
    >
    > enum FLAGS
    > {
    > flag1,
    > flag2,
    > flag3
    > ...
    > }
    > std::bitset<FLAGS> flags;


    This is wrong: std::bitset takes a (non-type) std::size_t parameter.

    > c)
    >
    > bool flag1;
    > bool flag2;
    > bool flag3;
    > ...


    This wastes space.

    >
    > Which way would you prefer?
    >


    d)

    namespace flags {
    const int flag1 = 1;
    const int flag2 = flag1 << 1;
    const int flag3 = flag2 << 1;
    ....
    }

    int flags;

    Jonathan
     
    Jonathan Turkanis, Nov 30, 2004
    #2
    1. Advertising

  3. "Jonathan Turkanis" <> wrote:

    > namespace flags {
    > const int flag1 = 1;
    > const int flag2 = flag1 << 1;
    > const int flag3 = flag2 << 1;
    > ....
    > }
    >
    > int flags;


    Whoops! I meant

    int flags_;

    Jonathan
     
    Jonathan Turkanis, Nov 30, 2004
    #3
  4. loquak wrote:
    > Greets.
    > I wonder which way would be the best for holding some state flags in terms
    > of memory use and efficiency.


    - here, memory use and efficiency are 2 very different things.

    It is undoubtedly faster to read and write bool or perhaps char values
    than it is to read an write individual bits.

    You need to ask yourself, is memory use more important than performance.

    Now, if you have such large numbers of bits, such that you're trashing
    cache all the time, you might find that a compact format is more
    interesting.

    Hence, I would probably choose a) if size was an issue, or c) if
    performance was more important.

    BTW - vector<bool> does employ a more compact format.

    >
    > a)
    >
    > struct FLAGS
    > {
    > bool flag1 : 1;
    > bool flag2 : 1;
    > bool flag3 : 1;
    > ...
    > }
    > FLAGS flags;
    >
    > b)
    >
    > enum FLAGS
    > {
    > flag1,
    > flag2,
    > flag3
    > ...
    > }
    > std::bitset<FLAGS> flags;
    >
    > c)
    >
    > bool flag1;
    > bool flag2;
    > bool flag3;
    > ...
    >
    > Which way would you prefer?
    >
    >
     
    Gianni Mariani, Nov 30, 2004
    #4
  5. Jonathan Turkanis wrote:

    >
    > "Jonathan Turkanis" <> wrote:
    >
    >> namespace flags {
    >> const int flag1 = 1;
    >> const int flag2 = flag1 << 1;
    >> const int flag3 = flag2 << 1;
    >> ....
    >> }
    >>
    >> int flags;

    >
    > Whoops! I meant
    >
    > int flags_;
    >
    > Jonathan


    What does the trailing underscore do? Or is it just a notation thing? Just
    curious.
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 30, 2004
    #5
  6. loquak

    PKH Guest

    "Jonathan Turkanis" <> wrote in message
    news:...
    >
    > "Jonathan Turkanis" <> wrote:
    >
    >> namespace flags {
    >> const int flag1 = 1;
    >> const int flag2 = flag1 << 1;
    >> const int flag3 = flag2 << 1;
    >> ....
    >> }
    >>
    >> int flags;

    >
    > Whoops! I meant
    >
    > int flags_;
    >
    > Jonathan
    >
    >


    One technique I read in the faq that is nice if you have more flags than can
    fit into an integer datatype :
    http://www.parashift.com/c -faq-lite/ctors.html [10.17] What is the "Named
    Parameter Idiom"?

    PKH
     
    PKH, Nov 30, 2004
    #6
  7. "Matthias K├Ąppler" <> wrote in message
    news:coh8hl$63f$04$-online.com...
    > Jonathan Turkanis wrote:
    >
    > >
    > > "Jonathan Turkanis" <> wrote:
    > >
    > >> namespace flags {
    > >> const int flag1 = 1;
    > >> const int flag2 = flag1 << 1;
    > >> const int flag3 = flag2 << 1;
    > >> ....
    > >> }
    > >>
    > >> int flags;

    > >
    > > Whoops! I meant
    > >
    > > int flags_;
    > >
    > > Jonathan

    >
    > What does the trailing underscore do? Or is it just a notation thing? Just
    > curious.


    I didn't want the namespace and the variable holding the flags to have the same
    name. Usually the variable consisting of a combination of flags will be a data
    member of some class; I usually call it "flags_".

    Jonathan
     
    Jonathan Turkanis, Nov 30, 2004
    #7
  8. "PKH" <> wrote in message
    news:VMVqd.19400$...
    >
    > "Jonathan Turkanis" <> wrote in message
    > news:...
    > >
    > > "Jonathan Turkanis" <> wrote:
    > >
    > >> namespace flags {
    > >> const int flag1 = 1;
    > >> const int flag2 = flag1 << 1;
    > >> const int flag3 = flag2 << 1;
    > >> ....
    > >> }
    > >>
    > >> int flags;

    > >
    > > Whoops! I meant
    > >
    > > int flags_;
    > >
    > > Jonathan
    > >
    > >

    >
    > One technique I read in the faq that is nice if you have more flags than can
    > fit into an integer datatype :
    > http://www.parashift.com/c -faq-lite/ctors.html [10.17] What is the "Named
    > Parameter Idiom"?


    David Abrahams and Daniel Wallin have a nice library which allows name
    parameters, using syntax like:

    Dog* d = new Dog(age = 3, weight = 45.1);

    It was just reviewed for inclusion in Boost; the result is pending.

    I don't really see the connection between this and bit flags, though.

    Jonathan
     
    Jonathan Turkanis, Nov 30, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant

    Session problem? not holding

    Grant, Aug 13, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    458
    Carl Prothman [MVP]
    Aug 14, 2003
  2. David
    Replies:
    0
    Views:
    339
    David
    Jun 21, 2006
  3. Steve Holden
    Replies:
    0
    Views:
    787
    Steve Holden
    Feb 8, 2009
  4. Eddy Xu
    Replies:
    5
    Views:
    129
    Eddy Xu
    Apr 11, 2008
  5. Alex J
    Replies:
    21
    Views:
    534
    Tim Rentsch
    Jun 16, 2013
Loading...

Share This Page