程序中的所有數(shù)在計(jì)算機(jī)內(nèi)存中都是以二進(jìn)制的形式儲(chǔ)存的。位運(yùn)算就是直接對(duì)整數(shù)在內(nèi)存中的二進(jìn)制位進(jìn)行操作。C語(yǔ)言中位運(yùn)算符: &(按位與)、| (按位或)、~(按位取反)、 <<(左移)、>>(右移) 、^(異或運(yùn)算)。
計(jì)算機(jī)所能識(shí)別的即為二進(jìn)制數(shù),位運(yùn)算就是按位運(yùn)算, 因而同等情況下位運(yùn)算效率一般都高于其它方式。
1.位運(yùn)算特性
unsigned char a=0x45,b=0x89; a=0x45(0100 0101) b=0x89(1000 1001)
位運(yùn)算 |
規(guī)則 |
示例 |
&(按位與) |
全1為1,有0則0 | a&b=0x1(0000 0001b) |
|(按位或) | 有1為1,全0則0 | a|b=0xcd(1100 1101b) |
~(按位取反) |
對(duì)每一位進(jìn)行取反,1變0,0變1 |
~a=0xba(1011 1010) ~b=0x76(0111 0110) |
<<(左移) |
高位溢出,低位補(bǔ)0 |
(a<<1)=0x8a(1000 1010) (b<<1)=0x12(0001 0010) |
>>(有移) |
低位溢出,高位補(bǔ)0 |
(a>>1)=0x22(0010 0010) (b>>1)=0x44(0100 0100) |
^(異或運(yùn)算) |
相同為0,不同為1 |
a^b=0xcc(1100 1100) |
- 優(yōu)先級(jí)關(guān)系:
~(取反)
<<(左移) ? >>(右移)
&按位與
^(按位異或)
|(按位或)
2.示例
#include
int main()
{
unsigned char a=0x45,b=0x89;
printf("與運(yùn)算a&b:%#xn",a&b);
printf("或運(yùn)算a|b:%#xn",a|b);
printf("取反運(yùn)算~a:%#xt ~b:%#xn",~a&0xff,~b&0xff);
printf("左移a<1:%#xt b<1:%#xn",(a<1)&0xff,(b<1)&0xff);
printf("右移a?>>1:%#xt b>>1:%#xn",a>>1,b>>1);
printf("異或a^b:%#xn",a^b);
}
[wbyq@wbyq 123]$ gcc main.c
[wbyq@wbyq 123]$ ./a.out
與運(yùn)算a&b:0x1
或運(yùn)算a|b:0xcd
取反運(yùn)算~a:0xba ~b:0x76
左移a<1:0x8a b<1:0x12
右移a?>>1:0x22 b>>1:0x44
異或a^b:0xcc
3.位運(yùn)算練習(xí)
1.定義兩個(gè)變量unsigned char a=0x45,b=0x89;將a的第6位清0,將b的第4位置1;
2.定義兩個(gè)整型變量a,b將a,b的值交換位置(不借助第三個(gè)變量,使用位運(yùn)算);
3.定義一個(gè)整型變量data,將字符'a'、'b'、'c'、'd' 保存到data中,并輸出字符abcd;
4.漢字點(diǎn)陣輸出
漢字顯示我們每時(shí)每刻都在使用,那漢字是如何顯示在屏幕上的呢?通過位運(yùn)算的學(xué)習(xí)我們即可通過printf模擬函數(shù)輸出顯示。
例如我們需要在一個(gè)8*8的點(diǎn)陣方格中顯示一個(gè)”中”字。可以看做如下:
一個(gè)有8個(gè)格子,剛好一個(gè)字節(jié)有8位,一個(gè)方格中有兩個(gè)狀態(tài):空白和顯紅。我們可以把空白的地方看做0,把顯示紅色的地方看做1,這樣整個(gè)8*8個(gè)格我們就可以用8個(gè)字節(jié)表示,坐標(biāo)為高位,右邊為低位,高位在前方式。表示如下:
第一行:0x08;
第二行:0x08;
第三行:0x3e;
第四行:0x2a;
第五行:0x2a;
第六行:0x3e;
第七行:0x08;
第八行:0x08;
通過位運(yùn)算實(shí)現(xiàn)如下:
#include
const unsigned char buff[]={0x08,0x08,0x3E,0x2A,0x2a,0x3e,0x08,0x08};
int main()
{
int i,j;
unsigned char temp;
for(i=0;i8;i++)
{
temp=buff[i];
for(j=0;j8;j++)/*一個(gè)字節(jié)有8位數(shù)據(jù)*/
{
if(temp&0x80)
{
printf("*");
}
else
{
printf(" ");
}
temp<=1;//繼續(xù)判斷下一位數(shù)據(jù)
}
printf("n");
}
}
5.漢字顯示練習(xí)
1.通過PCtoLCD2002工具實(shí)現(xiàn)漢字取模進(jìn)行取模,封裝漢字顯示函數(shù),以橫向和縱向兩種方式顯示。
#include
#define FONT_SIZE 24
const char buf[][24*24/8]=
{
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x70,0x00,0x00,0x70,0x00,0x00,0x30,0x00,0x00,0x31,0xC0,0x00,0x7F,0xE0,0x0F,0xF0,0xE0,0x0E,0x30,0xE0,0x06,0x3F,0xC0,0x07,0xF9,0xC0,0x06,0x31,0x80,0x06,0x31,0x80,0x06,0xFF,0x00,0x03,0xF0,0x00,0x00,0x70,0x00,0x00,0x30,0x18,0x00,0x30,0x0C,0x00,0x38,0x1C,0x00,0x1F,0xFC,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00},/*"電",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x80,0x00,0x7D,0xC0,0x01,0xE3,0xC0,0x00,0x07,0x00,0x00,0x06,0x00,0x00,0x3C,0x00,0x00,0x1C,0x1C,0x00,0x0F,0xFE,0x0F,0xFC,0x00,0x3C,0x0E,0x00,0x00,0x0E,0x00,0x00,0x0E,0x00,0x00,0x0E,0x00,0x00,0x0E,0x00,0x00,0x0E,0x00,0x00,0x0E,0x00,0x00,0x0E,0x00,0x00,0xFC,0x00,0x00,0x3C,0x00,0x00,0x18,0x00,0x00,0x00,0x00},/*"子",1*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x1C,0x00,0x00,0x1C,0xF0,0x01,0x9C,0x30,0x01,0xD8,0x00,0x01,0x99,0xE0,0x03,0xBF,0x80,0x03,0xF0,0x00,0x03,0x30,0x00,0x00,0x73,0x80,0x00,0x7F,0xC0,0x00,0xF3,0x80,0x00,0xFB,0x80,0x01,0xDF,0x00,0x01,0x8F,0x00,0x03,0x07,0x00,0x07,0x0F,0x80,0x0E,0x39,0xE0,0x18,0x60,0xF8,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00},/*"發(fā)",2*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,0x03,0x9C,0x00,0x03,0x0D,0xC0,0x03,0x0F,0x00,0x03,0x1E,0x60,0x03,0xF7,0xE0,0x33,0xE3,0xC0,0x1B,0x83,0xCC,0x1F,0x06,0xFC,0x03,0x1C,0x1C,0x03,0x00,0xE0,0x03,0x1F,0xC0,0x07,0x8D,0x80,0x07,0xCD,0x80,0x06,0xDD,0x80,0x0E,0x19,0x86,0x0C,0x31,0x86,0x18,0x71,0xCE,0x30,0xC0,0xFC,0x00,0x00,0x00,0x00,0x00,0x00},/*"燒",3*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x1C,0x00,0x00,0x18,0x00,0x00,0x38,0x00,0x00,0x3B,0xF0,0x03,0xFF,0x00,0x07,0xB0,0x00,0x00,0x70,0x00,0x00,0x60,0x00,0x00,0x6F,0xC0,0x00,0xFB,0x80,0x00,0xC3,0x80,0x01,0xF3,0x80,0x03,0xBB,0x00,0x03,0x0F,0x00,0x06,0x07,0x00,0x0C,0x0F,0x80,0x18,0x1D,0xC0,0x70,0x38,0xF0,0x01,0xE0,0x7F,0x00,0x00,0x00,0x00,0x00,0x00},/*"友",4*/
};
int main()
{
int i,j,k,x;
unsigned char temp;
for(i=0;i
-
計(jì)算機(jī)
+關(guān)注
關(guān)注
19文章
7494瀏覽量
87933 -
內(nèi)存
+關(guān)注
關(guān)注
8文章
3024瀏覽量
74038 -
C語(yǔ)言
+關(guān)注
關(guān)注
180文章
7604瀏覽量
136793 -
位運(yùn)算
+關(guān)注
關(guān)注
0文章
17瀏覽量
8422
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論