- 大部份的高階語言不需要bit operation而low-level coding時,像寫驅動程式、或pixel等級的graphic programming則需要bit operation.
- 在C裡,一個char為一個byte;一個byte,則為8個bits
- Bit structure可以用c的16進位數字代表,如: “0x36” ,則為 “00110110”的代表,16進位在printf() 是使用 %x來表示,8進位則為%o。
- &: bitwise and
- |: bitwise or
- ^ :bitwise xor
- <<: shift left 比較特別的利用是 I = j>> 3 ,則等於I = j * (2*2*2)
- >> :shift right 比較特別的到用是 I= j<< 3 ,則等於 I = j / (2*2*2)
- ~ :bitwise not
C 語言printf()並沒有能代表binary的符號,得自己寫個 int_to_bit()程式
設定bits
如:const int ERROR = 0x01
練習:寫一個clear bit的macro
#include <stdio.h>
#define clear_bit(x) (x)=(x)>>24;
main()
{
int a=0xAF;
clear_bit(a)
printf("%d\n",a);
return 0;
}