r/ProgrammerHumor • • Oct 07 '26

Meme wasteOfPreciousSpace

Post image
7.3k Upvotes

244 comments sorted by

View all comments

Show parent comment

25

u/VictoryMotel Oct 07 '26

This is completely ridiculous. You might only be thinking of a few flags in a struct but if you think about things like a black and white array / image, there are plenty of times you want to pack in bits.

for a whole lot of CPU cycles.

Absolutely not. Bit shifting is as cheap as it gets, accessing memory is expensive. You could do 1500 bit shifts in the time it takes to get a single byte from memory that isn't in the TLB. (You can't actually make a cpu only get a single byte though. It will always access two cache lines at a minimum, each being 64 bytes).

0

u/[deleted] Oct 08 '26

[deleted] — view deleted comment

4

u/master-goose-boy Oct 08 '26

Dude what are you talking about? bitshift operators are available natively on all OSs and they can be directly applied on various primitives. And depending on the use case, you can validate/verify the Byte with various bit operators. If you can pack bits that are largely going be read out it can optimize your code both on memory and compute.

So OP made the correct point you are kinda off topic.

3

u/newspeakisungood Oct 08 '26

The OS doesn’t supply the bit shift operator. The processor does.

-1

u/master-goose-boy Oct 08 '26

Yeah thats what “available” means here…

2

u/newspeakisungood Oct 08 '26

I can (and have) used it with no OS though. Implying that it’s a property of the OS is wrong.

2

u/master-goose-boy Oct 08 '26

Although it wasn’t really what I was trying to imply because that would mean I explicitly excluded microprocessors instruction set (which I did not) but now I see what you mean… Agree that the way you framed it is better.

1

u/VictoryMotel Oct 08 '26

What in the world is this reply? I didn't say anything about vector<bool> and I'm not asking for anything. Did you mean to reply to someone else? This makes no sense.