专注于服务器编程、网络编程

~~保持一颗平常心~~持之以恒~~
posts - 18, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

常用bit位操作函数

Posted on 2010-04-27 21:55 ~William~ 阅读(1303) 评论(0)  编辑 收藏 引用 所属分类: C语言基础

/*

@brief 测试一个数某一位是否为

@param 被测试数据

@param 测试位数

@return false:0 true: 1

*/

bool BitTest(int const *Base, int Offset)

{

     if (*Base>>Offset&0x01 == 1)

     {

         return 1;

     }

     else

         return 0;

}

 

/*

@brief 设置某一个位为

@param 被设置数据

@param 测试位数

@return

*/

bool BitSet(int *Base, int Offset)

{

     if (Base==NULL || Offset<0 && Offset>sizeof(int)*8-1)

         return false;

     int mask = 0x01;

     *Base = (*Base) | (mask<<Offset);

     return true;

}

 

/*

@brief 重置某一位为

@param 被设置数据

@param 测试位数

@return

*/

bool BitReset(int *Base, int Offset)

{

     if (Base==NULL || Offset<0 && Offset>sizeof(int)*8-1)

         return false;

     int mask = 0x01;

     *Base = (*Base) & ~(mask<<Offset);

     return true;

}

 

/*

@brief 取反某一位数据

@param 被重置数据

@param Offset

*/

bool BitReverse(int *Base, int Offset)

{

     if (Base==NULL || Offset<0 && Offset>sizeof(int)*8-1)

         return false;

     int mask = 0x01;

     if ((*Base>>Offset)&0x01 == 1)

         *Base = (*Base) & ~(mask<<Offset);

     else

         *Base = (*Base) | (mask<<Offset);

}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理