程序中的所有数在计算机内存中都是以二进制的形式储存的。位运算就是直接对整数在内存中的二进制位进行操作。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 < stdio.h >
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的点阵方格中显示一个”中”字。可以看做如下:
一个有8个格子,刚好一个字节有8位,一个方格中有两个状态:空白和显红。我们可以把空白的地方看做0,把显示红色的地方看做1,这样整个8*8个格我们就可以用8个字节表示,坐标为高位,右边为低位,高位在前方式。表示如下:
第一行:0x08;
第二行:0x08;
第三行:0x3e;
第四行:0x2a;
第五行:0x2a;
第六行:0x3e;
第七行:0x08;
第八行:0x08;
通过位运算实现如下:
#include < stdio.h >
const unsigned char buff[]={0x08,0x08,0x3E,0x2A,0x2a,0x3e,0x08,0x08};
int main()
{
int i,j;
unsigned char temp;
for(i=0;i< 8;i++)
{
temp=buff[i];
for(j=0;j< 8;j++)/*一个字节有8位数据*/
{
if(temp&0x80)
{
printf("*");
}
else
{
printf(" ");
}
temp< <=1;//继续判断下一位数据
}
printf("n");
}
}
5.汉字显示练习
1.通过PCtoLCD2002工具实现汉字取模进行取模,封装汉字显示函数,以横向和纵向两种方式显示。
#include < stdio.h >
#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< FONT_SIZE;i++)
{
for(k=0;k< 5;k++)
{
for(x=0;x< FONT_SIZE/8;x++)
{
temp=buf[k][i*FONT_SIZE/8+x];
for(j=0;j< 8;j++)/*一个字节有8位数据*/
{
if(temp&0x80)
{
printf("*");
}
else
{
printf(" ");
}
temp< <=1;//继续判断下一位数据
}
}
}
printf("n");
}
}
-
计算机
+关注
关注
19文章
7414浏览量
87698 -
内存
+关注
关注
8文章
2998浏览量
73874 -
C语言
+关注
关注
180文章
7597浏览量
136140 -
位运算
+关注
关注
0文章
16浏览量
8413
发布评论请先 登录
相关推荐
评论