Hacker News new | ask | show | jobs
by quikoa 56 days ago
> Also, single bits are rarely used for booleans because it requires more CPU power to extract a single bit.

Not necessarily, on modern CPUs memory access is often the bottleneck. If you have many bits storing these in a bitarray can be quite beneficial for performance.

2 comments

Only if you have an application which can somehow utilize an array of bits. It's been quite rare in my career to see that in practice. I know of only a few corner cases.

Regardless, due to the way most CPUs work, you almost certainly aren't saving anything by using a bit instead of a byte. When a CPU goes to load a memory address it'll not just get that address, but also the addresses around that address and shove them into cache. With the thought that "If you are loading 1 byte, you are likely loading 128 bytes". The cost savings really only comes into effect when you have more boolean values than bytes that fit in a cache line.

This was the idea behind std::vector<bool> which is a bit array with template specialization, and has been tha bane of C++ devs for decades.