[C] 將指定的某些位元設定成 0 , 1 或反相

  • 4095
  • 0
  • 2013-08-02

位元運算

 

#include <stdio.h>

#define setbit(value,x) (value |=(1<<x))
#define clrbit(value,x) (value &=~(1<<x))
#define revbit(value,x) (value ^=(1<<x))

int main()
{
    int a=0x00,b=0xFF,c=0xFF;

    printf("setbit = %02X\n",setbit(a,1));
    printf("clrbit = %02X\n",clrbit(b,0));
    printf("revbit = %02X\n",revbit(c,1));

    return 0;
}

 

 

輸出:

setbit = 02   
clrbit = FE
revbit = FD