Posted on 2010-04-27 21:55
~William~ 閱讀(1346)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
C語(yǔ)言基礎(chǔ)
/*
@brief 測(cè)試一個(gè)數(shù)某一位是否為
@param 被測(cè)試數(shù)據(jù)
@param 測(cè)試位數(shù)
@return false:0 true: 1
*/
bool BitTest(int const *Base, int Offset)
{
if
(*Base>>Offset&0x01
== 1)
{
return
1;
}
else
return
0;
}
/*
@brief 設(shè)置某一個(gè)位為
@param 被設(shè)置數(shù)據(jù)
@param 測(cè)試位數(shù)
@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 被設(shè)置數(shù)據(jù)
@param 測(cè)試位數(shù)
@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 取反某一位數(shù)據(jù)
@param 被重置數(shù)據(jù)
@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);
}