473,786 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which is the best way for holding flags?

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<FLA GS> flags;

c)

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

Which way would you prefer?
Jul 22 '05 #1
7 1873
"loquak" <li*******@hotm ail.com> wrote in message
news:co******** **@mordred.cc.j yu.fi...
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<FLA GS> 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
Jul 22 '05 #2

"Jonathan Turkanis" <te******@kanga roologic.com> 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
Jul 22 '05 #3
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<FLA GS> flags;

c)

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

Which way would you prefer?

Jul 22 '05 #4
Jonathan Turkanis wrote:

"Jonathan Turkanis" <te******@kanga roologic.com> 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.
Jul 22 '05 #5
PKH

"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message
news:31******** *****@uni-berlin.de...

"Jonathan Turkanis" <te******@kanga roologic.com> 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
Jul 22 '05 #6

"Matthias Käppler" <no****@digital raid.com> wrote in message
news:co******** *****@news.t-online.com...
Jonathan Turkanis wrote:

"Jonathan Turkanis" <te******@kanga roologic.com> 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
Jul 22 '05 #7

"PKH" <no************ @online.no> wrote in message
news:VM******** ************@ne ws4.e.nsc.no...

"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message
news:31******** *****@uni-berlin.de...

"Jonathan Turkanis" <te******@kanga roologic.com> 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
Jul 22 '05 #8

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

Similar topics

17
6153
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
7
4691
by: Bostonasian | last post by:
I have a table that contains transactional data. Such as site view by whom, when, which template, etc, etc... Everytime when I pulled the report, hh:mm:ss never matters. Only breakdown by dates, not time. Having read some chapters of Ralph Kimball's book, I am inspired to build "date" table with integer as primary key. Here's what I have for schema of transactional table. - viewed_customer_id int (4bytes)
13
2067
by: Mark A. Odell | last post by:
I write a lot of drivers and there is inevitably some hardware register or buffer descriptor field called 'flags'. The flags are defined, typically, as bit positions. I was thinking I could get some compile-time type checking when assigning to a flag field but I don't think I can. Here's what I was thinking: typedef unsigned long HwFlag; struct HwThing { SomeType fieldOne;
7
2222
by: ajm | last post by:
Hi All, given an object file is there any way (or tool) to determine which compiler created it ? tia, A.
2
3647
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
1
10549
by: Dwight.Dexter | last post by:
I have an enum variable. Is there some way to cast an int or BitArray and get my enum value? I have to communicate with an old C-style sever so I get an array of bits("0","1"). But I'm using an enum variable to store the data. public enum LaneCapabilities { MLT = 0, ACM = 1,
9
2435
by: John Salerno | last post by:
This is where my OO skills aren't quite honed yet. Some of you might remember the little game program I was working on for fun. I had several methods written to determine a character's class, race, etc. My question is, should I put all those questions in a Character base class (so that all subclasses inherit them and I can call them straight from the objects), or should I put them as static methods in a Validator type class? Which is...
9
7302
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would be 'better' to store an instance of this class in a session-variable, so it's available all the time and needs to be instanced only once. Is this, generally speaking, a good idea, storing objects in session-variables ? Do you guys ever use this...
1
3860
by: Alexander Korsunsky | last post by:
Hi! Is it possible to extract the mode flags of a stream (FILE* stream or fstream - object), without looking at how the stream was opened? What I mean by mode flags is wether the stream is opened in read only mode, write only, binary and so on. I did not find any functions in C, but I found something similar in C++, the flags() member functio of an fstream object. I've tried to check the flags on ios_base::binary ios_base::in binary...
0
9497
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
10164
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
10110
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
7515
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
6748
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.