this post was submitted on 15 May 2025
1150 points (98.6% liked)

Programmer Humor

23427 readers
1317 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] savedbythezsh 14 points 1 week ago (5 children)

Are you telling me that no compiler optimizes this? Why?

[–] [email protected] 34 points 1 week ago

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.

[–] [email protected] 23 points 1 week ago (1 children)

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.

[–] [email protected] 7 points 1 week ago (1 children)

Why is this a big mistake? I’m not a c++ person

[–] [email protected] 5 points 1 week ago (1 children)

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.

[–] [email protected] 2 points 1 week ago (1 children)

Could you provide an example?

[–] [email protected] 1 points 4 days ago

The biggest problem is that each element doesn't have a unique memory address; iterators aren't just pointers.

[–] [email protected] 5 points 1 week ago* (last edited 1 week ago) (1 children)

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)

[–] Croquette 4 points 1 week ago

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.

[–] [email protected] 4 points 1 week ago

CPUs don't read one bit a a time.

[–] [email protected] 1 points 1 week ago

They do, that's the optimisation.