views:

389

answers:

4

Hi!

let's say i need to store 8 bools in a struct, but i want to use for them only 1 byte together, then i could do something like this:

struct myStruct {
    bool b1:1;
    bool b2:1;
    bool b3:1;
    bool b4:1;
    bool b5:1;
    bool b6:1;
    bool b7:1;
    bool b8:1;
};

and with this i could do things like

myStruct asdf;
asdf.b3=true;
asdf.b4=false;
if(asdf.b1)
    ...

is this correct so far? (i don't know it actually, i never used bitfields before)

ok - but is it also possible to create a static array of 8 bools such that they will use only 8 bits but i will still be able to adress them by index?

something like

struct myStruct {
public:
    bool b[8]:8;
};

maybe? (with this, i get a error C2033)

thanks for the help!

+1  A: 

Wouldn't you rather use a byte data type to hold everything at once? Then you'd only have to use logical ANDs and ORs to get/put stuff into it. No struct required.

inked
A: 

For various reasons I don't think it's a good idea - you're basically trying to replicate the behaviour of vector<bool> which has proven to be not a good idea. If you're trying to do this only to save memory, I wouldn't bother. The overhead of accessing the various bools and extracting them from a bit field is likely to be a lot higher than what little memory you save unless you're extremely constrained by memory limits.

To answer your direct question, if you want to do the bool/bitfield thing you'll have to use your first approach.

Normally the legitimate and accepted use for the bitfield/bit twiddling approach is when you have to deal with hardware registers and are trying to either model a hardware register or actually access a hardware register after making it look like a memory location and superimposing a bit field structure over the register.

Timo Geusch
hey! yes it's about memory. the problem is, that i have to create many of those objects on the heap and it seams the size of my struct affects performance of my app linearily. cutting it down from 64bytes to 36 more than doubled it. so now i'm looking for ways to cut the size further down
Mat
+8  A: 

I would recommend using a bitset . That way you could simply declare:

bitset<8> asdf;

and use it with [].

asdf[0] = true;
asdf[3] = false;
Dan Hook
if my researches are correct, bitset uses at least 4 bytes
Mat
As T.E.D. pointed out, there is a very good chance that any data structure will take up 32 bits when alignment issues are taken into account.
Dan Hook
There's some great links to explain alignment issues in another question: http://stackoverflow.com/questions/381244/purpose-of-memory-alignment
Dan Hook
Bitsets are 32 bytes for a reason, it's far faster in all cases. Except possibly the case where there is a large array of bitsets, but then I would just make a bigger bitset?
caspin
+1  A: 

You may be able to get your compiler to do what you want, but sadly it isn't required. For example, even a nice compiler that accepts the above might end up allocating an entire 32-bit word for your myStruct objects.

If you have the option, and you want that level of control over your types and how they are aligned and allocated, you should probably consider using Ada. For example, the following works just fine in Ada:

type Bit_Set is array (1..8) of Boolean;
for Bit_Set'size use 8;

High_Mask : constant Bit_Set := (1..7 => false, 8 => true);

...and you now have a single-byte bitmask, and the operators "and", "or", "xor", etc. that work bitwise with it.

T.E.D.
+1 for raising alignment issue; -1 for suggesting change of programming language. net 0 :)
lavinio
Fair enough. I just get so frustrated w/ C's limitations I can't help but mentioning that they aren't nessecary.
T.E.D.