程序中的所有數在計算機內存中都是以二進制的形式儲存的。位運算就是直接對整數在內存中的二進制位進行操作。C語言中位運算符: &(按位與)、| (按位或)、~(按位取反)、 <<(左移)、>>(右移) 、^(異或運算)。
計算機所能識別的即為二進制數,位運算就是按位運算, 因而同等情況下位運算效率一般都高于其它方式。
1.位運算特性
unsigned char a=0x45,b=0x89; a=0x45(0100 0101) b=0x89(1000 1001)
位運算 |
規則 |
示例 |
&(按位與) |
全1為1,有0則0 | a&b=0x1(0000 0001b) |
|(按位或) | 有1為1,全0則0 | a|b=0xcd(1100 1101b) |
~(按位取反) |
對每一位進行取反,1變0,0變1 |
~a=0xba(1011 1010) ~b=0x76(0111 0110) |
<<(左移) |
高位溢出,低位補0 |
(a<<1)=0x8a(1000 1010) (b<<1)=0x12(0001 0010) |
>>(有移) |
低位溢出,高位補0 |
(a>>1)=0x22(0010 0010) (b>>1)=0x44(0100 0100) |
^(異或運算) |
相同為0,不同為1 |
a^b=0xcc(1100 1100) |
- 優先級關系:
~(取反)
<<(左移) ? >>(右移)
&按位與
^(按位異或)
|(按位或)
2.示例
#include
int main()
{
unsigned char a=0x45,b=0x89;
printf("與運算a&b:%#xn",a&b);
printf("或運算a|b:%#xn",a|b);
printf("取反運算~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
與運算a&b:0x1
或運算a|b:0xcd
取反運算~a:0xba ~b:0x76
左移a<1:0x8a b<1:0x12
右移a?>>1:0x22 b>>1:0x44
異或a^b:0xcc
3.位運算練習
1.定義兩個變量unsigned char a=0x45,b=0x89;將a的第6位清0,將b的第4位置1;
2.定義兩個整型變量a,b將a,b的值交換位置(不借助第三個變量,使用位運算);
3.定義一個整型變量data,將字符'a'、'b'、'c'、'd' 保存到data中,并輸出字符abcd;
4.漢字點陣輸出
漢字顯示我們每時每刻都在使用,那漢字是如何顯示在屏幕上的呢?通過位運算的學習我們即可通過printf模擬函數輸出顯示。
例如我們需要在一個8*8的點陣方格中顯示一個”中”字??梢钥醋鋈缦拢?/p>
一個有8個格子,剛好一個字節有8位,一個方格中有兩個狀態:空白和顯紅。我們可以把空白的地方看做0,把顯示紅色的地方看做1,這樣整個8*8個格我們就可以用8個字節表示,坐標為高位,右邊為低位,高位在前方式。表示如下:
第一行:0x08;
第二行:0x08;
第三行:0x3e;
第四行:0x2a;
第五行:0x2a;
第六行:0x3e;
第七行:0x08;
第八行:0x08;
通過位運算實現如下:
#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++)/*一個字節有8位數據*/
{
if(temp&0x80)
{
printf("*");
}
else
{
printf(" ");
}
temp<=1;//繼續判斷下一位數據
}
printf("n");
}
}
5.漢字顯示練習
1.通過PCtoLCD2002工具實現漢字取模進行取模,封裝漢字顯示函數,以橫向和縱向兩種方式顯示。
#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},/*"發",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
-
計算機
+關注
關注
19文章
7488瀏覽量
87848 -
內存
+關注
關注
8文章
3019瀏覽量
74002 -
C語言
+關注
關注
180文章
7604瀏覽量
136683 -
位運算
+關注
關注
0文章
17瀏覽量
8422
發布評論請先 登錄
相關推薦
評論