C ch11重點整理-Bit Operation

来源:互联网 发布:路由器mac什么意思 编辑:程序博客网 时间:2024/04/29 13:31

  • 大部份的高階語言不需要bit operationlow-level coding時,像寫驅動程式、或pixel等級的graphic programming則需要bit operation.
  • C裡,一個char為一個byte;一個byte,則為8bits
  • Bit structure可以用c16進位數字代表,如: “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

      語言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;

}

0 0