472,790 Members | 3,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,790 software developers and data experts.

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 1829
"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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.