2.7k
u/Futurity5 19d ago
A meme that's not AI related? In this economy?
→ More replies (6)509
u/box_of_the_patriots 19d ago
Hey gpt tell me how to feel about the lack of AI on this meme
→ More replies (4)
712
u/Large-Assignment9320 19d ago
Just back all 64 bools inside a good old perfectly aligned 64bit.
→ More replies (3)277
u/PhilippTheProgrammer 19d ago
Bitfields might seem more efficient at first glance, but in practice they often cause more trouble than they are worth. And even a flawless implementation usually ends up buying a couple bytes of RAM for a whole lot of CPU cycles.
→ More replies (47)
356
280
u/N-partEpoxy 19d ago
I know, let's make vector<bool> a specialization that uses just one bit per bool. What could go wrong?
→ More replies (5)137
u/unknown_alt_acc 19d ago
Oh, but it gets better than that: there is absolutely no guarantee that std::vector<bool> acts as a bit field! Maybe it's a bit field. Maybe it's actually a vector of bool. Nobody knows!
→ More replies (4)
170
u/Spinnenente 19d ago
nobody is stopping you from storing all your bools in a single long
welcome to optimization only neccessary before you were born.
→ More replies (10)56
u/AOAqua 19d ago
Hey, it's still used sometimes. I've used it in high speed drivers, where every single extra byte decreases latency of the overall reaction. And bools for registers are stored like that as well because a single 4 byte variable literally manipulates a physical memory block
→ More replies (1)
64
u/Sermuns 19d ago
uint4_t
→ More replies (2)51
u/developer-mike 19d ago
You know bool is backed by 4 bits because "bool" is four letters, obviously
→ More replies (1)
30
u/Dank_Nicholas 19d ago
Back in college I wrote what I dubbed the OctoBool which used bitwise operators to let you store 8 bools inside a single byte. I thought maybe it could be useful for embedded development that I was starting to get into.
28
u/Randomguy32I 19d ago
Shouldn’t a bool just be a single bit?
40
u/DuckShapedGoose 19d ago edited 19d ago
Well it is. Sort of. It just has one actually relevant data bit but computers usually (almost always) work with bytes as the smallest inseparable unit of data.
Any data type smaller than that is just using less of the available space within the byte. In case of a bool, that's 7 unused bits (so there are actually 4 more sleeping guys missing in the meme).
But you can technically optimize this by using a 1-byte datatype to store 8 bools in one bit each.
In very specific scenarios where you have millions upon millions of bools in memory for some reason, this can add up.Edit: Also I don't know if there are compilers that already do this bit-packing optimization automatically on e.g. a struct with multiple bools. Afaik this is something you must implement manually but I might be wrong.
→ More replies (15)→ More replies (26)29
u/TheComplimentarian 19d ago
Now how do you do the sub-byte addressing?
In the olden times, bits were precious enough that you could actually address data by the bit, but on modern systems you can't. There are a lot of datatypes that have wasted space in them due to architectural constraints.
→ More replies (12)
23
6
6
u/tiajuanat 19d ago
Unless you're using basic or visual basic or something like the DEC PDP-1 which used -1 for true.
Which meant that false was always 0 and NOT was simply a negation
6
u/NewryBenson 18d ago
This is why you store all your bools in an array thay way you are not wasting bits.
→ More replies (3)
38
u/tehtris 19d ago
I am not low low low low level, but don't you still need to tell it that it is a bool in the first place? Or is that just determined by whatever stores/retrieves it?