[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Question about creating a struct of flags in c++

Plissken.s@gmail.com

10/30/2008 4:07:00 AM

Is there an efficient way to create a struct of flag in C++?

I need to create a struct of boolean flag, like this:
struct testStruct {
bool flag1;
bool flag2;
bool flag3;
bool flag4;
bool flag5;
bool flag6;
bool flag7;
bool flag8;
bool flag9;
bool flag10;
bool flag11;
bool flag12;
bool flag13;
};

But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.

Is it possible to create a struct so that each flag uses only 1 bit.

Thank you.
20 Answers

Victor Bazarov

10/30/2008 4:11:00 AM

0

Plissken.s@gmail.com wrote:
> Is there an efficient way to create a struct of flag in C++?
>
> I need to create a struct of boolean flag, like this:
> struct testStruct {
> bool flag1;
> bool flag2;
> bool flag3;
> bool flag4;
> bool flag5;
> bool flag6;
> bool flag7;
> bool flag8;
> bool flag9;
> bool flag10;
> bool flag11;
> bool flag12;
> bool flag13;
> };
>
> But if I do that, i print out the sizeof(), that struct and it is 13.
> So i think the compile use 1 byte for each flag.
>
> Is it possible to create a struct so that each flag uses only 1 bit.

Yes, read about bitfields. The syntax is the colon and the field width
after the name of the member, like

bool flag1:1;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Plissken.s@gmail.com

10/30/2008 5:50:00 AM

0

On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Plisske...@gmail.com wrote:
> > Is there an efficient way to create a struct of flag in C++?
>
> > I need to create a struct of boolean flag, like this:
> > struct testStruct {
> >    bool flag1;
> >    bool flag2;
> >    bool flag3;
> >    bool flag4;
> >    bool flag5;
> >    bool flag6;
> >    bool flag7;
> >    bool flag8;
> >    bool flag9;
> >    bool flag10;
> >    bool flag11;
> >    bool flag12;
> >    bool flag13;
> > };
>
> > But if I do that, i print out the sizeof(), that struct and it is 13.
> > So i think the compile use 1 byte for each flag.
>
> > Is it possible to create a struct so that each flag uses only 1 bit.
>
> Yes, read about bitfields.  The syntax is the colon and the field width
> after the name of the member, like
>
>      bool flag1:1;
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

Thank you Victor.

I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};

Can I set all the flag to 0 by doing this:

myMask mask;

memset(&mask, '\0', sizeof(myMask));

And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));

Thanks.

Hisense

10/30/2008 8:07:00 AM

0

On 10?30?, ??1?50?, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
> On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
>
>
>
>
> > Plisske...@gmail.com wrote:
> > > Is there an efficient way to create a struct of flag in C++?
>
> > > I need to create a struct of boolean flag, like this:
> > > struct testStruct {
> > > bool flag1;
> > > bool flag2;
> > > bool flag3;
> > > bool flag4;
> > > bool flag5;
> > > bool flag6;
> > > bool flag7;
> > > bool flag8;
> > > bool flag9;
> > > bool flag10;
> > > bool flag11;
> > > bool flag12;
> > > bool flag13;
> > > };
>
> > > But if I do that, i print out the sizeof(), that struct and it is 13.
> > > So i think the compile use 1 byte for each flag.
>
> > > Is it possible to create a struct so that each flag uses only 1 bit.
>
> > Yes, read about bitfields. The syntax is the colon and the field width
> > after the name of the member, like
>
> > bool flag1:1;
>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask
>
> Thank you Victor.
>
> I am going to follow you suggestion. If I create a class like this:
> class myMask {
> public:
> bool flag1:1;
> bool flag2:1;
> bool flag3:1;
> bool flag4:1;
> bool flag5:1;
> bool flag6:1;
> bool flag7:1;
> bool flag8:1;
> bool flag9:1;
> bool flag10:1;
> bool flag11:1;
> bool flag12:1;
> bool flag13:1;
> };
>
> Can I set all the flag to 0 by doing this:
>
> myMask mask;
>
> memset(&mask, '\0', sizeof(myMask));
>
> And can I compare if 2 masks are the same by doing this:
>
> myMask mask1;
> myMask mask2;
>
> memcmp(&mask1, &mask2, sizeof(myMask));
>
> Thanks.- ??????? -
>
> - ??????? -

there are two things to say:
1, you need no class, but struct, because struct is good enough to
solve it;
2, either class or struct, you could use ctor where all the bit fields
are set to 0 or any value you want.

anon

10/30/2008 12:42:00 PM

0

Hisense wrote:
>>
>> I am going to follow you suggestion. If I create a class like this:
>> class myMask {
>> public:
>> bool flag1:1;
>> bool flag2:1;
>> bool flag3:1;
>> bool flag4:1;
>> bool flag5:1;
>> bool flag6:1;
>> bool flag7:1;
>> bool flag8:1;
>> bool flag9:1;
>> bool flag10:1;
>> bool flag11:1;
>> bool flag12:1;
>> bool flag13:1;
>> };
>>
>> Can I set all the flag to 0 by doing this:
>>
>> myMask mask;
>>
>> memset(&mask, '\0', sizeof(myMask));
>>

Proper way is:

myMask mask = myMask();

>> And can I compare if 2 masks are the same by doing this:
>>
>> myMask mask1;
>> myMask mask2;
>>
>> memcmp(&mask1, &mask2, sizeof(myMask));

No, because of padding, some bits can be left uninitialized, therefore
you have to compare each field.
I am not sure if you can do this (probably not)
if(mask1 == mask2)
{
....
}

> 2, either class or struct, you could use ctor where all the bit fields
> are set to 0 or any value you want.

POD structures can not have constructors. Read here:
http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/do...

James Kanze

10/30/2008 2:14:00 PM

0

On Oct 30, 6:50 am, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
> On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
>
>
> > Plisske...@gmail.com wrote:
> > > Is there an efficient way to create a struct of flag in C++?
>
> > > I need to create a struct of boolean flag, like this:
> > > struct testStruct {
> > > bool flag1;
> > > bool flag2;
> > > bool flag3;
> > > bool flag4;
> > > bool flag5;
> > > bool flag6;
> > > bool flag7;
> > > bool flag8;
> > > bool flag9;
> > > bool flag10;
> > > bool flag11;
> > > bool flag12;
> > > bool flag13;
> > > };
>
> > > But if I do that, i print out the sizeof(), that struct and it is 13.
> > > So i think the compile use 1 byte for each flag.
>
> > > Is it possible to create a struct so that each flag uses only 1 bit.
>
> > Yes, read about bitfields. The syntax is the colon and the field width
> > after the name of the member, like
>
> > bool flag1:1;
>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask
>
> Thank you Victor.
>
> I am going to follow you suggestion. If I create a class like this:
> class myMask {
> public:
> bool flag1:1;
> bool flag2:1;
> bool flag3:1;
> bool flag4:1;
> bool flag5:1;
> bool flag6:1;
> bool flag7:1;
> bool flag8:1;
> bool flag9:1;
> bool flag10:1;
> bool flag11:1;
> bool flag12:1;
> bool flag13:1;
> };
>
> Can I set all the flag to 0 by doing this:
>
> myMask mask;
>
> memset(&mask, '\0', sizeof(myMask));
>
> And can I compare if 2 masks are the same by doing this:
>
> myMask mask1;
> myMask mask2;
>
> memcmp(&mask1, &mask2, sizeof(myMask));
>
> Thanks.

Victor Bazarov

10/30/2008 2:22:00 PM

0

Plissken.s@gmail.com wrote:
> [..] If I create a class like this:
> class myMask {
> public:
> bool flag1:1;
> bool flag2:1;
> bool flag3:1;
> bool flag4:1;
> bool flag5:1;
> bool flag6:1;
> bool flag7:1;
> bool flag8:1;
> bool flag9:1;
> bool flag10:1;
> bool flag11:1;
> bool flag12:1;
> bool flag13:1;
> };
>
> Can I set all the flag to 0 by doing this:
>
> myMask mask;

That will leave them undefined. Consider defining the default
constructor and the comparison operator.

>
> memset(&mask, '\0', sizeof(myMask));
>
> And can I compare if 2 masks are the same by doing this:
>
> myMask mask1;
> myMask mask2;
>
> memcmp(&mask1, &mask2, sizeof(myMask));

It is better to have the overloaded operator== in your class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Plissken.s@gmail.com

10/30/2008 3:03:00 PM

0

On Oct 30, 9:22 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Plisske...@gmail.com wrote:
> > [..]  If I create a class like this:
> > class myMask {
> > public:
> >      bool flag1:1;
> >      bool flag2:1;
> >      bool flag3:1;
> >      bool flag4:1;
> >      bool flag5:1;
> >      bool flag6:1;
> >      bool flag7:1;
> >      bool flag8:1;
> >      bool flag9:1;
> >      bool flag10:1;
> >      bool flag11:1;
> >      bool flag12:1;
> >      bool flag13:1;
> >   };
>
> > Can I set all the flag to 0 by doing this:
>
> >     myMask mask;
>
> That will leave them undefined.  Consider defining the default
> constructor and the comparison operator.
>
>
>
> >     memset(&mask, '\0', sizeof(myMask));
>
> > And can I compare if 2 masks are the same by doing this:
>
> > myMask mask1;
> > myMask mask2;
>
> > memcmp(&mask1, &mask2, sizeof(myMask));
>
> It is better to have the overloaded operator== in your class.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

Thank you for all the help.

In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
* compare if 2 classes have the same values (the same set of flags are
set)

Thank you again.

.rhavin grobert

10/30/2008 3:04:00 PM

0

On 30 Okt., 06:50, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
> On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
> > Plisske...@gmail.com wrote:
> > > Is there an efficient way to create a struct of flag in C++?
>
> > > I need to create a struct of boolean flag, like this:
> > > struct testStruct {
> > >    bool flag1;
> > >    bool flag2;
> > >    bool flag3;
> > >    bool flag4;
> > >    bool flag5;
> > >    bool flag6;
> > >    bool flag7;
> > >    bool flag8;
> > >    bool flag9;
> > >    bool flag10;
> > >    bool flag11;
> > >    bool flag12;
> > >    bool flag13;
> > > };
>
> > > But if I do that, i print out the sizeof(), that struct and it is 13.
> > > So i think the compile use 1 byte for each flag.
>
> > > Is it possible to create a struct so that each flag uses only 1 bit.
>
> > Yes, read about bitfields.  The syntax is the colon and the field width
> > after the name of the member, like
>
> >      bool flag1:1;
>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask
>
> Thank you Victor.
>
> I am going to follow you suggestion. If I create a class like this:
> class myMask {
> public:
>      bool flag1:1;
>      bool flag2:1;
>      bool flag3:1;
>      bool flag4:1;
>      bool flag5:1;
>      bool flag6:1;
>      bool flag7:1;
>      bool flag8:1;
>      bool flag9:1;
>      bool flag10:1;
>      bool flag11:1;
>      bool flag12:1;
>      bool flag13:1;
>   };
>
> Can I set all the flag to 0 by doing this:
>
>     myMask mask;
>
>     memset(&mask, '\0', sizeof(myMask));
>
> And can I compare if 2 masks are the same by doing this:
>
> myMask mask1;
> myMask mask2;
>
> memcmp(&mask1, &mask2, sizeof(myMask));

try this:

struct myMask {
union {
unsigned int nMask;
struct {
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};
};
};

inline bool operator==(myMask const& rm, myMask const& lm)
{return rm.nMask == lm.nMask;};

the union will force your compiler to put all the bits into one int.
you can the set the int to 0 to clear all bits and compare the ints to
compare the masks.

Just a hint: you'll never need '/0'. just write 0. '/0' is just used
to inform the readers of your code: "hey folks, this is explicitely
meant as a char in a string". so if you want to do something weired:

int i = 20;
while (i-->'\0')
{
if (i == '/2')
{
CallSomeMessageFunc("I've reached 2!");
}
}

.rhavin grobert

10/30/2008 3:07:00 PM

0

On 30 Okt., 16:03, ".rhavin grobert" <cl...@yahoo.de> wrote:
> On 30 Okt., 06:50, "Plisske...@gmail.com" <Plisske...@gmail.com>
> wrote:
>
>
>
> > On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
> > > Plisske...@gmail.com wrote:
> > > > Is there an efficient way to create a struct of flag in C++?
>
> > > > I need to create a struct of boolean flag, like this:
> > > > struct testStruct {
> > > >    bool flag1;
> > > >    bool flag2;
> > > >    bool flag3;
> > > >    bool flag4;
> > > >    bool flag5;
> > > >    bool flag6;
> > > >    bool flag7;
> > > >    bool flag8;
> > > >    bool flag9;
> > > >    bool flag10;
> > > >    bool flag11;
> > > >    bool flag12;
> > > >    bool flag13;
> > > > };
>
> > > > But if I do that, i print out the sizeof(), that struct and it is 13.
> > > > So i think the compile use 1 byte for each flag.
>
> > > > Is it possible to create a struct so that each flag uses only 1 bit.
>
> > > Yes, read about bitfields.  The syntax is the colon and the field width
> > > after the name of the member, like
>
> > >      bool flag1:1;
>
> > > V
> > > --
> > > Please remove capital 'A's when replying by e-mail
> > > I do not respond to top-posted replies, please don't ask
>
> > Thank you Victor.
>
> > I am going to follow you suggestion. If I create a class like this:
> > class myMask {
> > public:
> >      bool flag1:1;
> >      bool flag2:1;
> >      bool flag3:1;
> >      bool flag4:1;
> >      bool flag5:1;
> >      bool flag6:1;
> >      bool flag7:1;
> >      bool flag8:1;
> >      bool flag9:1;
> >      bool flag10:1;
> >      bool flag11:1;
> >      bool flag12:1;
> >      bool flag13:1;
> >   };
>
> > Can I set all the flag to 0 by doing this:
>
> >     myMask mask;
>
> >     memset(&mask, '\0', sizeof(myMask));
>
> > And can I compare if 2 masks are the same by doing this:
>
> > myMask mask1;
> > myMask mask2;
>
> > memcmp(&mask1, &mask2, sizeof(myMask));
>
> try this:
>
> struct myMask {
>   union {
>     unsigned int nMask;
>     struct {
>       bool flag1:1;
>       bool flag2:1;
>       bool flag3:1;
>       bool flag4:1;
>       bool flag5:1;
>       bool flag6:1;
>       bool flag7:1;
>       bool flag8:1;
>       bool flag9:1;
>       bool flag10:1;
>       bool flag11:1;
>       bool flag12:1;
>       bool flag13:1;
>    };
>   };
>
> };
>
> inline bool operator==(myMask const& rm, myMask const& lm)
> {return rm.nMask == lm.nMask;};
>
> the union will force your compiler to put all the bits into one int.
> you can the set the int to 0 to clear all bits and compare the ints to
> compare the masks.
>
> Just a hint: you'll never need '/0'. just write 0. '/0' is just used
> to inform the readers of your code: "hey folks, this is explicitely
> meant as a char in a string". so if you want to do something weired:
>
> int i = 20;
> while (i-->'\0')
> {
>   if (i == '/2')
>   {
>     CallSomeMessageFunc("I've reached 2!");
>   }
>
> }
>
>

typo: " if (i == '/2') " should be " if (i == '\2') "

sean_in_raleigh

10/30/2008 4:25:00 PM

0


It's not exactly on point, but you might be
interested in this article about doing
efficient, typesafe bit-twiddling
with C++: http://accu.org/index.php/jo...

Sean