返回:贺老师课程教学链接
阅读并运行下面的程序,掌握按位运算的规则。
(1)
#include "stdio.h"
int main()
{
char x=040;
printf("%d\n",x=x<<1);
return 0;
}
(2)
#include "stdio.h"
int main()
{
unsigned int a,b;
a=7^3;
b=~4&3;
printf("%d,%d\n",a,b);
return 0;
}
(3)
#include "stdio.h"
int main()
{
char x=040;
printf("%o\n",x<<1);
return 0;
}
(4)
#include "stdio.h"
int main()
{
char a=0x95,b,c;
b=(a&0xf)<<4;
c=(a&0xf0)>>4;
a=b|c;
printf("%x\n",a);
return 0;
}
(5)
#include "stdio.h"
int main()
{
unsigned char a=2,b=4,c=5,d;
d = a|b;
d &= c;
printf("%d\n",d);
return 0;
}
(6)
#include "stdio.h"
struct bit
{
unsigned a:2; //这里定义的称之为位域,2代表占一个字节中的2位
unsigned b:3; //掌握详情,请搜索“位域”
unsigned c:4;
int i;
} data;
int main()
{
data.b=2, data.a=8; //因为data.a只占2位,所以赋值为8后,只保存8的后2位,结果为0
printf("%d %d\n",data.a, data.b);
return 0;
}