using bools storage question

Has there ever or is there now an attempt to stuff 8 bools into one byte. This would save an immense amount storage. A bool only needs one bit, are they wasting the other 7? I have always been curious and nobody seems to know. Very few even know what I'm talking about!

You can roll your own "8 'booleans' in a byte" function but they won't be real booleans of course

Can you take the address of a bit?
No.

Does that answer your question?

If you want to store eight flags in a byte, go ahead.
But it isn't a bool.

The old mainframes had a test under mask instruction, but not these things I guess. Thanks for replying.

Yes there are bitfields in C++.
https://en.cppreference.com/w/cpp/language/bit_field

Packing eight bool values into a byte would be more efficient in terms of memory for the data itself, but would likely be more than offset by the increase in code size needed to implement it, unless the processor has a specific set of instructions to efficiently handle that type of data structure.

You could use the bitSet(), bitClear(), and bitRead() functions for a similar effect, but again that would not be a bool.

Yes, it's a trade off between code size/efficiency and memory usage. So the choice does depend on how many bools you actually have. It's often advantageous to test a boolean rapidly, and most processors do not have instructions that can test individual bits (although some do). That is mainly why a boolean false is defined as 0, and a boolean true is non-zero. 99.999% of all processors have a machine code instruction that can perform that test in one machine cycle.

If you are sending bools through a slow communications channel, obviously you do want to pack the bits somehow.

https://en.cppreference.com/w/cpp/container/vector_bool

std::vector is what you are looking for.

That's interesting. Beyond what I need but hey they tried. I noticed going thru my stuff that I was moving int to a bool. I fixed it but I wonder if bool is allocated as int.

sevenoutpinball:
I fixed it but I wonder if bool is allocated as int.

sizeof (bool) suggests not.

sevenoutpinball:
That's interesting. Beyond what I need but hey they tried. I noticed going thru my stuff that I was moving int to a bool. I fixed it but I wonder if bool is allocated as int.

A bool is allocated as a bool. Both bool and int are fundamental types.

There are implicit conversions from integers to bool:

Boolean conversions
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

https://en.cppreference.com/w/cpp/language/implicit_conversion#Boolean_conversions

The fixed-size equivalent of std::vector<bool> is std::bitset, which might be more appropriate in many embedded contexts.

Pieter

so moving anything but zero to a bool moves a 1 to it. that might be handy.

XC8 used on PICs supports the non standard bit variable type, which behaves like bool but only uses 1 bit. I assume it relies on the bit test machine code instructions available on a PIC.

PerryBebbington:
XC8 used on PICs supports the non standard bit variable type, which behaves like bool but only uses 1 bit. I assume it relies on the bit test machine code instructions available on a PIC.

What makes you think AVR doesn't have a bit test instruction?

BST (Bit Store from Bit in Register to T Bit in SREG) extracts a single bit from a register and sets the T flag to its value. You can then branch on this value using BRTC (Branch if the T Bit is Cleared) or BRTS (Branch if the T Bit is Set), or store the bit back in a register using BLD (Bit Load from the T Bit in SREG to a Bit in Register).

You also have SBRS (Skip if Bit in Register is Set) and SBRC (Skip if Bit in Register is Cleared).

PieterP:
What makes you think AVR doesn't have a bit test instruction?

What makes you think I have an opinion on whether it does or does not? I don't know either way, well, I do now but only because you just told me. My guess would have been that it probably does, but I have never checked.