Posted on 2010-04-27 21:55
~William~ 閱讀(1344)
評論(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);
}