In my opinion, one bit is all you ever need for a binary variable like bool
. Is it in any way a bad decision to explicitly tell all bool
s to use only 1 bit?
struct Banana {
// little fields
bool on_off : 1;
bool yes_no : 1;
bool left_right : 1;
bool alive_dead : 1;
bool in_out : 1;
};
Edit:
I know that it is not possible to take the address of a field. Any other downsides?
If you have LOTS of these things, it saves space. But it does add at least an extra AND
or OR
instruction for each clear/set/check operation over the normal one-byte solution.
In the whole scheme of things, unless you actually have a HUGE number, there is probably no benefit.
There's a time/space/synchronisation trade off.
Clearly you can store 32 times as many bits in the same space.
However, to access an individual bool you need at least a masking operation, and probably a shift (though under certain circumstances, that is likely to be optimised out).
This has consequences if multiple threads of control attempt to modify booleans as you've changed a simple write for your update to a read/modify/write so now you have to add synchronisation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With