This reminds me that I actually once made a class to store bools packed in uint8 array to save bytes.
Had forgotten that. I think i have to update the list of top 10 dumbest things i ever did.
I mean is it really a waste? What’s minimum amount of bits most CPUs read in one cycle.
In terms of memory usage it’s a waste. But in terms of performance you’re absolutely correct. It’s generally far more efficient to check is a word is 0 than to check if a single bit is zero.
I swore I read that mysql dbs will store multiple bools in a row as bit maps in one byte. I can’t prove it though
We need to be able to express 0 and 1 as integers so that functionality is just being overloaded to express another concept.
Wait until the person who made this meme finds out about how many bits are being wasted on modern CPU architectures. 7 is the minimum possible wasted bits but it would be 31 on every modern computer (even 64b machines since they default to 32b ints).
boolean bloat
I first thought you wrote boolean float, not sure if that’s even worse.
boolean root beer float
Are you telling me that no compiler optimizes this? Why?
CPUs don’t read one bit a a time.
It would be slower to read the value if you had to also do bitwise operations to get the value.
But you can also define your own bitfield types to store booleans packed together if you really need to. I would much rather that than have the compiler do it automatically for me.
Consider what the disassembly would look like. There’s no fast way to do it.
It’s also unnecessary since 8 bytes is a negligible amount in most cases. Serialization is the only real scenario where it matters. (Edit: and embedded)
In embedded, if you are to the point that you need to optimize the bools to reduce the footprint, you fucked up sizing your mcu.
Well there are containers that store booleans in single bits (e.g.
std::vector<bool>
- which was famously a big mistake).But in the general case you don’t want that because it would be slower.
Why is this a big mistake? I’m not a c++ person
The mistake was that they created a type that behaves like an array in every case except for
bool
, for which they created a special magical version that behaves just subtly different enough that it can break things in confusing ways.Could you provide an example?
In the industrial automation world and most of the IT industry, data is aligned to the nearest word. Depending on architecture, that’s usually either 16, 32, or 64 bits. And that’s the space a single Boolean takes.
That’s why I primarily use booleans in return parameters, beyond that I’ll try to use bitfields. My game engine’s tilemap format uses a 32 bit struct, with 16 bit selecting the tile, 12 bit selecting the palette, and 4 bit used for various bitflags (horizontal and vertical mirroring, X-Y axis invert, and priority bit).
Bit fields are a necessity in low level networking too.
They’re incredibly useful, I wish more people made use of them.
I remember I interned at a startup programming microcontrollers once and created a few bitfields to deal with something. Then the lead engineer went ahead and changed them to masked ints. Because. The most aggravating thing is that an int size isn’t consistent across platforms, so if they were ever to change platforms to a different word length, they’d be fucked as their code was full of platform specific shenanigans like that.
/rant
I always use stdint.h so that my types are compatible across any mcu. And it makes the data type easily known instead of guessing an i t size
Good rant.
Yeah. I once had to do stuff to code that had bit-fields like that and after a while, realised (by means of StackOverflow) that that part is UB and I had to go with bitwise operations instead.
Undefined Behavior…?
Ok, I recalled wrong, it was unspecified
pragma(pack) {
int a:1, b:1, … h:1;
}
IIRC.
Back in the day when it mattered, we did it like
#define BV00 (1 << 0) #define BV01 (1 << 1) #define BV02 (1 << 2) #define BV03 (1 << 3) ...etc #define IS_SET(flag, bit) ((flag) & (bit)) #define SET_BIT(var, bit) ((var) |= (bit)) #define REMOVE_BIT(var, bit) ((var) &= ~(bit)) #define TOGGLE_BIT(var, bit) ((var) ^= (bit)) ....then... #define MY_FIRST_BOOLEAN BV00 SET_BIT(myFlags, MY_FIRST_BOOLEAN)
Edit - oops, responded to wrong comment…
Okay. Gen z programmer here. Can you explain this black magic? I see it all the time in kernel code but I have no idea what it means.
It’s called bitshifting and is used to select which bits you want to modify so you can toggle them individually.
1 << 0 is the flag for the first bit
1 << 1 for the second
1 << 2 for the third and so onI think that’s correct. It’s been years since I’ve used this technique tbh 😅
Which part?
With embedded stuff its still done like that. And if you go from the arduino functionss to writing the registers directly its a hell of a lot faster.
Wait till you find out about alignment and padding
3GPP has an interesting way of serialising bools on the wire with ASN.1
NULL OPTIONAL
meaning only the type would be stored if true, otherwise it won’t be set at all
That requires some form of self describing format and will probably look like a sparse matrix in the end.
It’s far more often stored in a word, so 32-64 bytes, depending on the target architecture. At least in most languages.
No it isn’t. All statically typed languages I know of use a byte. Which languages store it in an entire 32 bits? That would be unnecessarily wasteful.
True.
Well storing that would only take half a bit.
if wasting a byte or seven matters to you, then then you need to be working in a lower level language.
It’s 7 bits…
Pay attention. 🤪
7 bytes! Look at Mr. Moneybags here!
Well when it comes to bytes, you could say I’m a bit of a millionaire myself.
typedef struct { bool a: 1; bool b: 1; bool c: 1; bool d: 1; bool e: 1; bool f: 1; bool g: 1; bool h: 1; } __attribute__((__packed__)) not_if_you_have_enough_booleans_t;
This was gonna be my response to OP so I’ll offer an alternative approach instead:
typedef enum flags_e : unsigned char { F_1 = (1 << 0), F_2 = (1 << 1), F_3 = (1 << 2), F_4 = (1 << 3), F_5 = (1 << 4), F_6 = (1 << 5), F_7 = (1 << 6), F_8 = (1 << 7), } Flags; int main(void) { Flags f = F_1 | F_3 | F_5; if (f & F_1 && f & F_3) { // do F_1 and F_3 stuff } }
Why not
if (f & (F_1 | F_3)) {
? I use this all the time in embedded code.edit: never mind; you’re checking for both flags. I’d probably use
(f & (F_1 | F_3)) == (F_1 | F_3)
but that’s not much different than what you wrote.
Or just
std::bitset<8>
for C++. Bit fields are neat though, it can store weird stuff like a 3 bit integer, packed next to booleansThat’s only for C++, as far as I can tell that struct is valid C
You beat me to it!