请教有关位段的一个问题
#include <iostream.h>
enum ALPHA{a,b,c,d};
class SWITCH //总数23位,紧凑方式编SWITCH大小为4个字节
{
int power:3;
int water:5;
int oil:6;
int alarm:5;
int manual:4;
};
union STATE //总数9位,紧凑方式编STATE大小为2个字节
{
int start:1;
int speed:5;
ALPHA stop:1;
unsigned run:2;
};
void main(void)
{
cout << "The size of int is " << sizeof(int) << " bytes\n";
cout << "The size of SWITH is " << sizeof(SWITCH) << " bytes\n";
cout << "The size of STATE is " << sizeof(STATE) << " bytes\n";
}
答案是2 3 2, 为什么? 是怎么计算的呢?