位操作,获取和设置特定位的值,效率

来源:互联网 发布:知乎专栏 kindle 推送 编辑:程序博客网 时间:2024/06/06 01:38


http://tieba.baidu.com/p/2900558125



#define SetBit(LPByte,BitPlace,BitValue) ( (*LPByte) = ( (*LPByte)&~(1<<(BitPlace-1) ))|(BitValue<<(BitPlace-1)) );


#define GetBit(LPByte,BitPlace,BitValue) ( BitValue=((*LPByte)&(1<<(BitPlace-1)))>>(BitPlace-1) );



#define GetNBit (LPByte,Begin,End,BitValue) (BitValue=((*LPByte)&((255>>(8-End))&(255<<(Begin-1))))>>(Begin-1));




#define SetNBit (LPByte,Begin,End,BitValue) (*LPByte)=((*LPByte)&(~((255>>(8-End))&(255<<(Begin-1)))))|(BitValue<<(Begin-1));


一次处理N位,可以一次设置N位,获取N位,当Begin=End时获取和设置一位

0 0