473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Efficient way to store a limited number of booleans

Hi,

I was trying to make an entry in a std::map be stored more
efficiently. The struct contained 3 booleans discribing it's property,
so I am trying to make it as compact as possible.
Using a std::vector<boolin this case does not work, or at least is
not as efficient for my particular case (size was 20 bytes). Same goes
for tr1::array<bool,3>.

Is there anything else that I could have reused ?

Here is the current code:

// Compact struct to store up to 8 booleans:
struct B3
{
template <unsigned int TPos>
void SetB(bool v) {
if( v ) b.b |= (0x80 >TPos%8);
else b.b &= (~(0x80 >TPos%8));
}
template <unsigned int TPos>
bool GetB() const {
return b.b & (0x80 >(TPos%8));
}
private:
union { unsigned char b; } b;
};

Thanks
-Mathieu
Dec 11 '07 #1
8 1381
On 2007-12-11 12:13:39 -0500, mathieu <ma***************@gmail.comsaid:
>
Is there anything else that I could have reused ?
Let the compiler do the work:

struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;
};

eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 11 '07 #2
On Dec 11, 6:16 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-11 12:13:39 -0500, mathieu <mathieu.malate...@gmail.comsaid:
Is there anything else that I could have reused ?

Let the compiler do the work:

struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;

};

eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;
s/unsigned/unsigned char/g

Much nicer/cleaner indeed !

Thanks
-Mathieu
Dec 11 '07 #3
mathieu wrote:
On Dec 11, 6:16 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2007-12-11 12:13:39 -0500, mathieu <mathieu.malate...@gmail.com>
said:
>> Is there anything else that I could have reused ?

Let the compiler do the work:

struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;

};

eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;

s/unsigned/unsigned char/g
Does it make a real difference? I mean, did you check the
'sizeof' for the resulting struct?
Much nicer/cleaner indeed !
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 11 '07 #4
On Dec 11, 6:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
On Dec 11, 6:16 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-11 12:13:39 -0500, mathieu <mathieu.malate...@gmail.com>
said:
> Is there anything else that I could have reused ?
Let the compiler do the work:
struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;
};
eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;
s/unsigned/unsigned char/g

Does it make a real difference? I mean, did you check the
'sizeof' for the resulting struct?
At least on gcc 4.1.2 (debian) it does. With unsigned int I get 4
(obviously) and with unsigned char I get 1 as expected (same size as
my solution but cleaner).
-Mathieu
Dec 11 '07 #5
On Dec 11, 6:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
On Dec 11, 6:16 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-11 12:13:39 -0500, mathieu <mathieu.malate...@gmail.com>
said:
> Is there anything else that I could have reused ?
Let the compiler do the work:
struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;
};
eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;
s/unsigned/unsigned char/g

Does it make a real difference? I mean, did you check the
'sizeof' for the resulting struct?
Actually you are raising a good point. Shouldn't I be doing:
struct eight_bools
{
bool bool0 : 1;
bool bool1 : 1;
bool bool2 : 1;
bool bool3 : 1;
bool bool4 : 1;
bool bool5 : 1;
bool bool6 : 1;
bool bool7 : 1;
};

Thanks
-Mathieu
Dec 11 '07 #6
On Dec 11, 10:13 pm, mathieu <mathieu.malate...@gmail.comwrote:
Hi,

I was trying to make an entry in a std::map be stored more
efficiently. The struct contained 3 booleans discribing it's property,
so I am trying to make it as compact as possible.
Using a std::vector<boolin this case does not work, or at least is
not as efficient for my particular case (size was 20 bytes). Same goes
for tr1::array<bool,3>.

Is there anything else that I could have reused ?

Here is the current code:

// Compact struct to store up to 8 booleans:
struct B3
{
template <unsigned int TPos>
void SetB(bool v) {
if( v ) b.b |= (0x80 >TPos%8);
else b.b &= (~(0x80 >TPos%8));
}
template <unsigned int TPos>
bool GetB() const {
return b.b & (0x80 >(TPos%8));
}
private:
union { unsigned char b; } b;
};
How about using std::bitset<3>?
Dec 12 '07 #7
On Dec 11, 6:16 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-11 12:13:39 -0500, mathieu <mathieu.malate...@gmail.comsaid:
Is there anything else that I could have reused ?
Let the compiler do the work:
struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;
};
eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;
If he's putting this into a map, it probably won't take anymore
data space than a tr1::array< bool >, or a bool[3]. (I think he
said he only had three bools.) Whatever structure he uses will
be placed in a map node, which will also contain the key and a
couple of pointers. And be aligned and padded for pointers, at
least---on a typical machine, that means that anything less than
four bytes will in fact occupy four (or eight) bytes.

In fact, using the tr1::array<boolwill actually result in a
small space saving, because the size of the code needed to read
and write the values will be smaller.

If there's more data than just the 3 bool's, of course, bit
fields may be just the solution. Throw in a couple of ints with
very limited ranges, and the size gain can be very significant.

But of course, if he's worried about memory use, std::map may
not be the data structure he needs.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 12 '07 #8
Abhishek Padmanabh wrote:
On Dec 11, 10:13 pm, mathieu <mathieu.malate...@gmail.comwrote:
>Hi,

I was trying to make an entry in a std::map be stored more
efficiently. The struct contained 3 booleans discribing it's
property, so I am trying to make it as compact as possible.
Using a std::vector<boolin this case does not work, or at least
is not as efficient for my particular case (size was 20 bytes). Same
goes for tr1::array<bool,3>.

Is there anything else that I could have reused ?

Here is the current code:

// Compact struct to store up to 8 booleans:
struct B3
{
template <unsigned int TPos>
void SetB(bool v) {
if( v ) b.b |= (0x80 >TPos%8);
else b.b &= (~(0x80 >TPos%8));
}
template <unsigned int TPos>
bool GetB() const {
return b.b & (0x80 >(TPos%8));
}
private:
union { unsigned char b; } b;
};

How about using std::bitset<3>?
What is 'sizeof(std::bitset<3>)' on your machine? Just curious...

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

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

Similar topics

3
by: Sean | last post by:
Hi all I have a bit of a dilema that I am hoping some of you smart dudes might be able to help me with. 1. I have a table with about 50 million records in it and quite a few columns. 2. I...
4
by: Bayou BoB | last post by:
Hello; Simple question for most I'm sure. I'm curious how many records MS Access will store in a single database? What happens when you reach that number of records? Do you just lose the ability...
9
by: Bill | last post by:
I need to create a temporary file on the file system. I have over 5000 users What's the best method to ensure uniqueness Thank Bill
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
6
by: chech | last post by:
Possible to have array of booleans? Dim b1 As Boolean, b2 As Boolean, b3 As Boolean Dim obj() As Object = {b1, b2, b3} dim v As Object For Each v In obj v = True Next This does not work. ...
2
by: Alan Silver | last post by:
Hello, I am designing a form that allows people to request the formation of a limited company. When they fill in the form, they have to supply a certain amount of information relevant to their...
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
2
by: eriwik | last post by:
I'm working on an application that performs calculations on triangles of a 3D-model. As part of those computations I have to calculate a value for each pair of triangles and the number of triangles...
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.