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

前端 未结 7 2011
滥情空心
滥情空心 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:23

    Because a 32 bit Intel-compatible processor cannot access bytes individually (or better, it can by applying implicitly some bit mask and shifts) but only 32bit words at time.

    if you declare

    bitset<4> a,b,c;
    

    even if the library implements it as char, a,b and c will be 32 bit aligned, so the same wasted space exist. But the processor will be forced to premask the bytes before letting bitset code to do its own mask.

    For this reason MS used a int[1+(N-1)/32] as a container for the bits.

提交回复
热议问题