Why is std::bitset<8> 4 bytes big?

前端 未结 7 2024
滥情空心
滥情空心 2021-01-18 17:54

It seems for std::bitset<1 to 32>, the size is set to 4 bytes. For sizes 33 to 64, it jumps straight up to 8 bytes. There can\'t be any overhead because std::bitset<

相关标签:
7条回答
  • 2021-01-18 18:50

    The memory system on modern machines cannot fetch anything else but words from memory, apart from some legacy functions that extract the desired bits. Hence, having the bitsets aligned to words makes them a lot faster to handle, because you do not need to mask out the bits you don't need when accessing it. If you do not mask, doing something like

    bitset<4> foo = 0;
    if (foo) {
        // ...
    }
    

    will most likely fail. Apart from that, I remember reading some time ago that there was a way to cramp several bitsets together, but I don't remember exactly. I think it was when you have several bitsets together in a structure that they can take up "shared" memory, which is not applicable to most use cases of bitfields.

    0 讨论(0)
提交回复
热议问题