How does a bit field work with character types?

前端 未结 3 482
栀梦
栀梦 2020-12-11 19:21
   struct stats
   {
       char top         : 1; 
       char bottom      : 1;
       char side        : 2;
   } MyStat;  

I have seen this format

3条回答
  •  半阙折子戏
    2020-12-11 19:34

    Char bit fields work in the same way as int, just the base type is 8-bit wide, not 32-bit. So you'd get a struct stats, which has the size of 1 byte, and 3 member variables, occupying a total of 4 bits.

提交回复
热议问题