473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create 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<FLAGS> flags;

c)

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

Which way would you prefer?
Jul 22 '05 #1
7 1857
"loquak" <li*******@hotmail.com> wrote in message
news:co**********@mordred.cc.jyu.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<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
Jul 22 '05 #2

"Jonathan Turkanis" <te******@kangaroologic.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<FLAGS> flags;

c)

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

Which way would you prefer?

Jul 22 '05 #4
Jonathan Turkanis wrote:

"Jonathan Turkanis" <te******@kangaroologic.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******@kangaroologic.com> wrote in message
news:31*************@uni-berlin.de...

"Jonathan Turkanis" <te******@kangaroologic.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****@digitalraid.com> wrote in message
news:co*************@news.t-online.com...
Jonathan Turkanis wrote:

"Jonathan Turkanis" <te******@kangaroologic.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********************@news4.e.nsc.no...

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

"Jonathan Turkanis" <te******@kangaroologic.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
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....
7
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,...
13
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...
7
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
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
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...
9
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,...
9
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...
1
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...
0
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,...
0
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...
0
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,...
0
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...
1
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...
0
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...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.