1. 程式人生 > >按位定義結構體

按位定義結構體

按位定義一個結構體
struct   s_DosAttributes
{
unsigned   ReadOnly     :1;
unsigned   Hidden         :1;
unsigned   System         :1;

unsigned   VolumeID     :1;
unsigned   Directory   :1;
unsigned   Archive       :1;
unsigned   reserved     :2;
};

這個結構共佔用4個位元組,   ReadOnly佔用Bit0,   Hidden佔用Bit1...reserve佔用Bit7和Bit6
比如
struct   s_DosAttributes   st;
memset(&st,   0,   sizeof(st));
st.ReadOnly   =   1;
st.VolumeID   =   1;
st.reserved   =   3;
unsigned   char   c;
memcpy(&c,   &st,   sizeof(c));
這時c   的值是0xc9;