設置或清除指定的位

#include
using namespace std;
/**********************************************************
//設置或清除指定的位
//設置指定的第三位 使用 宏 #define BIT3 由 0x1 左移三位
//假如指定的是 第五位 使用 宏 #define BIT5 由 0x1 左移五位
//當然 也可以使用其他字母組合來定義
***********************************************************/
#define BIT3 (0x1 << 3)
static int a; // static 設置初值時 默認爲0
void set_bit3(void)
{
 a|=BIT3; //將第三位 設置爲 1
}
void clear_bit3(void)
{
 a&=~BIT3; //將第三位 設置爲 0
}
int main()
{
 printf("%d\n",a);
 set_bit3();
 printf("%d\n",a);
 clear_bit3();
 printf("%d\n",a); 
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章