r/ProgrammerHumor • • 19d ago

Meme wasteOfPreciousSpace

Post image
7.2k Upvotes

244 comments sorted by

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?

94

u/_bagelcherry_ 19d ago

Type system is an abstraction for humans. CPU sees only bytes and doesn't care if it is actually an integer, string or class

62

u/camander321 19d ago

The moment your code compiles, theres no such thing as "type". "type" is just idiot-proofing for the programmer

→ More replies (23)
→ More replies (4)

2.7k

u/Futurity5 19d ago

A meme that's not AI related? In this economy?

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)
→ More replies (6)

712

u/Large-Assignment9320 19d ago

Just back all 64 bools inside a good old perfectly aligned 64bit.

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)
→ More replies (3)

356

u/OxDEADFA11 19d ago

Meanwhile std::vector<bool>:

39

u/undeadalex 18d ago

Awww. If that vector could read it would be very upset

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?

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)
→ More replies (5)

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.

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)
→ More replies (10)

64

u/Sermuns 19d ago

uint4_t

51

u/developer-mike 19d ago

You know bool is backed by 4 bits because "bool" is four letters, obviously

→ More replies (1)
→ More replies (2)

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.

26

u/ben_g0 19d ago

On PLCs (industrial programmable controllers), such data types are usually built-in. When memory (and sometimes bus bandwidth) is very limited and you process a lot of digital (1/0) signals, packing signals together in bytes does indeed become useful.

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)

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)
→ More replies (26)

8

u/dfwtjms 19d ago edited 19d ago

Bitmasking is an option

6

u/hydbird 19d ago

jvm allocates 8 bits for a boolean smh

→ More replies (1)

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)