473,606 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about creating a struct of flags in c++

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.
Oct 30 '08 #1
20 3402
Pl********@gmai l.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
Oct 30 '08 #2
On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Plisske...@gmai l.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.
Oct 30 '08 #3
On 10ÔÂ30ÈÕ, ÏÂÎç1ʱ50·Ö, "Plisske...@gma il.com" <Plisske...@gma il.com>
wrote:
On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:


Plisske...@gmai l.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.
Oct 30 '08 #4
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(&mask 1, &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-gro...x/doc/POD.html
Oct 30 '08 #5
On Oct 30, 6:50 am, "Plisske...@gma il.com" <Plisske...@gma il.com>
wrote:
On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Plisske...@gmai l.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.
Oct 30 '08 #6
Pl********@gmai l.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
Oct 30 '08 #7
On Oct 30, 9:22*am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Plisske...@gmai l.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.
Oct 30 '08 #8
On 30 Okt., 06:50, "Plisske...@gma il.com" <Plisske...@gma il.com>
wrote:
On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Plisske...@gmai l.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==(myMa sk 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')
{
CallSomeMessage Func("I've reached 2!");
}
}
Oct 30 '08 #9
On 30 Okt., 16:03, ".rhavin grobert" <cl...@yahoo.de wrote:
On 30 Okt., 06:50, "Plisske...@gma il.com" <Plisske...@gma il.com>
wrote:
On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Plisske...@gmai l.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==(myMa sk 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')
* {
* * CallSomeMessage Func("I've reached 2!");
* }

}

typo: " if (i == '/2') " should be " if (i == '\2') "
Oct 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1408
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
36
7745
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption about the most restrictive type on my machine? Thanks.
6
2190
by: Arthur J. O'Dwyer | last post by:
As far as I know, C89/C90 did not contain the now-standard offsetof() macro. Did C89 mandate that structs had to have a consistent layout? For example, consider the typical layout of the following structure: struct weird { int x; /* sizeof(int)==4 here */
7
7844
by: mike | last post by:
Hi, I am having difficulty in creating a thread using pthread_create. It seems that pthread_create does not execute 'program', and returns -1; I have checked the API but I am not sure why this isn't working. #include <stdio.h> #include <pthread.h>
11
1664
by: nishant | last post by:
I've a structure: struct msghdr { //some goes here struct iovec *msg_iov; //some more --- }
18
2433
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in compatibility problems. The contents of the jmp_buf buffer are specific
7
4497
by: Daniel Rudy | last post by:
Is the following code legal? typedef stuct data_type_tag { int var1; int var2; int var3; struct { void *p; size_t size; unsigned int flags;
10
2263
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it for non-trivial tasks, I always happen to write small wrapper functions to adjust the values returned by struct. An example API would be the following: ============================================
12
4628
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each identifier between inside and outside struct block.... typedef struct evConn { evConnFunc func; void * uap;
0
8036
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8448
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5470
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3948
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2454
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.