这些函数操作“位”,经常用到:
-
-
void SetN1(unsigned short *pBuf, int n)
-
{
-
(*pBuf) |= 1<<n;
-
}
-
-
-
void SetN0(unsigned short *pBuf, int n)
-
{
-
(*pBuf) &= ~(1<<n);
-
}
-
-
-
void SetN1(unsigned char *pBuf, int n)
-
{
-
(*pBuf) |= 1<<n;
-
}
-
-
-
void SetN0(unsigned char *pBuf, int n)
-
{
-
(*pBuf) &= ~(1<<n);
-
}
-
-
-
int Get1Bit(unsigned char buf, int n)
-
{
-
return (buf>>n) & 0x01;
-
}
-
-
-
int Get1Bit(unsigned short buf, int n)
-
{
-
return (buf>>n) & 0x01;
-
}
-
-
-
int Get2Bit(unsigned char buf, int n)
-
{
-
return (buf>>n) & 0x03;
-
}
-
-
-
int Get2Bit(unsigned short buf, int n)
-
{
-
return (buf>>n) & 0x03;
-
}
-
-
-
void Set2Bit(unsigned short *pBuf, int n, int m)
-
{
-
(*pBuf) |= m<<n;
-
}
其实,最方便操纵“位”,如果不限制C语言的话,使用C++的STL中bitset最方便。
阅读(1288) | 评论(0) | 转发(0) |