位操作的函数模板,通用于各整数类型,实现如下。
//获取数据的位状态
template<typename T>
inline BOOL GetBit(T t, UINT nBit)
{
return ((t >> nBit) & 1);
}
//将某一位设置为1
template<typename T>
inline void SetBitOn(T *t, UINT nBit)
{
*t |= 0x01 << nBit;
}
//设置某一位为0
template<typename T>
inline void SetBitOff(T *t, UINT nBit)
{
*t &= ~(0x01<< nBit);
}
//将某一位设置为0或1
template<typename T>
void SetBit(T *t, UINT nBit, BOOL bFlag)
{
bFlag ? SetBitOn(t, nBit) : SetBitOff(t, nBit);
}本站部分资源收集于网络,纯个人收藏,无商业用途,如有侵权请及时告知!