在C下可以很方便的用位域共同体实现位状态操作。借鉴不少网友的方法,测试一下使用属性实现此功能。
//方法函数
privatestatic bytegetdatx(object _v,byte_t,int _x)
{
intdatt = Convert.ToInt16(_t);
if((int)_v == 1)
datt |= 1 << _x;
else
datt &= ~(1 << _x);
return(byte)datt;
}
struct TEMPDAT
{
publicbyte dat;
publicint b0
{
get{ return ((dat & 0x01) == 0x01) ? 1 : 0; }
set{dat= getdatx(value, dat,0);}
}
publicint b1
{
get{ return ((dat & 0x02) == 0x02) ? 1 : 0; }
set {dat = getdatx(value, dat, 1); }
}
publicint b2
{
get{ return ((dat & 0x04) == 0x04) ? 1 : 0; }
set{ dat = getdatx(value, dat, 2); }
}
publicint b3
{
get{ return ((dat & 0x08) == 0x08) ? 1 : 0; }
set{ dat = getdatx(value, dat, 3); }
}
publicint b4
{
get{ return ((dat & 0x10) == 0x10) ? 1 : 0; }
set {dat = getdatx(value, dat, 4); }
}
publicint b5
{
get{ return ((dat & 0x20) == 0x20) ? 1 : 0; }
set{ dat = getdatx(value, dat, 5); }
}
publicint b6
{
get{ return ((dat & 0x40) == 0x40) ? 1 : 0; }
set{ dat = getdatx(value, dat, 6); }
}
publicint b7
{
get{ return ((dat & 0x80) == 0x80) ? 1 : 0; }
set{ dat = getdatx(value, dat, 7); }
}
}
privateTEMPDAT tempDat;
代码写的有些冗余,期待共同学习,共同提高。