#include <stdio.h>
#include <iostream.h>

int main(void)
{
    typedef struct{
            unsigned short a:8;
            unsigned short b:9;
            unsigned short c:10;
        }T_struct1;
        typedef struct{
            unsigned short a:2;
            unsigned short b:10;
            unsigned short c:4;
            unsigned short d:10;
            unsigned short e:6;
        }T_struct2;

        typedef struct{
            unsigned short a:2;
            unsigned short b:10;
            unsigned short c:10;
            unsigned short d:4;
            unsigned short e:6;
        }T_struct3;

        cout<<sizeof(T_struct1)<<endl;
        cout<<sizeof(T_struct2)<<endl;
        cout<<sizeof(T_struct3)<<endl;
    
    return 0;
}

执行的结果是:
6
4
6
解释:
struct中unsigned short为16bit,2字节宽。
对T_struct1,占据的内存为:2(只用了8bit)+2(只用了9bit)+2(只用了10bit)
对T_struct2,占据的内存为:2((2+10+2)bit)+2((10+6)bit)
对T_struct3,占据的内存为:2((2+10)bit)+2((10+4)bit)+2(6bit)

相关文章:

  • 2022-01-26
  • 2022-12-23
  • 2021-06-04
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
猜你喜欢
  • 2021-12-19
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2021-09-29
  • 2021-07-11
  • 2022-01-17
相关资源
相似解决方案