一、位运算
1. 写出以下程序的运行结果。
#include <iostream>
#define BIT_MASK(bit_pos) (0x01 << (bit_pos))
int Bit_Reset(unsigned int *val, unsigned char pos)
{
if(pos >= sizeof(unsigned int ) * 8)
return 0;
*val = (*val & ~BIT_MASK(pos));
return 1;
}
int main()
{
unsigned int x = 0xffffffff;
unsigned char y = 4;
Bit_Reset(&x, y);
std::cout << std::hex << x << '\n';
return 0;
}
#define BIT_MASK(bit_pos) (0x01 << (bit_pos))
int Bit_Reset(unsigned int *val, unsigned char pos)
{
if(pos >= sizeof(unsigned int ) * 8)
return 0;
*val = (*val & ~BIT_MASK(pos));
return 1;
}
int main()
{
unsigned int x = 0xffffffff;
unsigned char y = 4;
Bit_Reset(&x, y);
std::cout << std::hex << x << '\n';
return 0;
}
2. 写一个八进制数转化为十进制数的程序。
3. 下面程序的运行结果是什么。
#include <iostream>
using namespace std;
int main()
{
unsigned short int i = 0;
int j = 8, p;
p = j << 1;
i = i -1;
cout << "i=" << i << endl;
cout << "p=" << p << endl;
return 0;
}
using namespace std;
int main()
{
unsigned short int i = 0;
int j = 8, p;
p = j << 1;
i = i -1;
cout << "i=" << i << endl;
cout << "p=" << p << endl;
return 0;
}
4. 以下程序的运行结果是什么?
#include <iostream>
using namespace std;
union{
unsigned char a;
unsigned int i;
}u;
int main()
{
u.i = 0xabCDef;
cout << hex << u.i <<endl;
cout << hex << int(u.a) <<endl;
return 0;
}
using namespace std;
union{
unsigned char a;
unsigned int i;
}u;
int main()
{
u.i = 0xabCDef;
cout << hex << u.i <<endl;
cout << hex << int(u.a) <<endl;
return 0;
}
5. 嵌入式系统总是要用户对变量或寄存器进行位操作。给定一个整形变量a,写两段代码,第一个设置a的bit3,第二个清除a的bit3。在以上两个操作中,要保持其他位不变。
#include <stdio.h>
#define BIT3 (0x1 << 3)
static int a=5;
void set_bit3(void)
{
a |= BIT3;
}
void clear_bit3(void)
{
a &= ~BIT3;
}
int main()
{
set_bit3();
printf("%d \n", a);
clear_bit3();
printf("%d \n", a);
return 0;
}
#define BIT3 (0x1 << 3)
static int a=5;
void set_bit3(void)
{
a |= BIT3;
}
void clear_bit3(void)
{
a &= ~BIT3;
}
int main()
{
set_bit3();
printf("%d \n", a);
clear_bit3();
printf("%d \n", a);
return 0;
}
6. 用<<、>>、|、&实现一个WORD(ABCD)的高低位交换。
#include <stdio.h>
int main()
{
unsigned short a=0xABCD;
unsigned short b;
unsigned short c, d;
b=(a<<8)&0xff00;
c=(a>>8)&0x00ff;
d=b|c;
printf("%#X \n", b);
printf("%#X \n", c);
printf("%#X \n", d);
return 0;
}
int main()
{
unsigned short a=0xABCD;
unsigned short b;
unsigned short c, d;
b=(a<<8)&0xff00;
c=(a>>8)&0x00ff;
d=b|c;
printf("%#X \n", b);
printf("%#X \n", c);
printf("%#X \n", d);
return 0;
}
tip
按位与:1保持,0清除
按位或:0保持,1设置
二、嵌入式编程
1. 测试你是否懂机器字长。
#include <stdio.h>
int main()
{
unsigned int zero = 0;
unsigned int compzero1 = 0xFFFF; /* incorrect assignment */
unsigned int compzero2 = ~0; /* 1 is complement of zero */
printf("%#X \n", zero);
printf("%#X \n", compzero1);
printf("%#X \n", compzero2);
return 0;
}
int main()
{
unsigned int zero = 0;
unsigned int compzero1 = 0xFFFF; /* incorrect assignment */
unsigned int compzero2 = ~0; /* 1 is complement of zero */
printf("%#X \n", zero);
printf("%#X \n", compzero1);
printf("%#X \n", compzero2);
return 0;
}
2. 如何设置一绝对地址为0x0012ff7c的整型变量的值为0xAABB。编译器是一个纯粹的ANSI编译器。
#include <stdio.h>
int main()
{
*(int *)0x0012ff7c = 0xaabb;
printf("%x", *(int *)0x0012ff7c);
return 0;
}
int main()
{
*(int *)0x0012ff7c = 0xaabb;
printf("%x", *(int *)0x0012ff7c);
return 0;
}
3. 以下代码的输出是什么?为什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int len1, len2;
char *p1, *p2, *p3;
if((p1 = (char *)malloc(0)) == NULL)
puts("Got a null pointer");
else
puts("Got a valid pointer");
if((len1 = (strlen(p2 = (char *)malloc(0)))) == 0)
puts("Got a null pointer");
else
puts("Got a valid pointer");
if((len2 = sizeof(p3 = (char *)malloc(0))) == 4)
puts("Got a null pointer");
else
puts("Got a valid pointer");
return 0;
}
#include <stdlib.h>
#include <string.h>
int main()
{
int len1, len2;
char *p1, *p2, *p3;
if((p1 = (char *)malloc(0)) == NULL)
puts("Got a null pointer");
else
puts("Got a valid pointer");
if((len1 = (strlen(p2 = (char *)malloc(0)))) == 0)
puts("Got a null pointer");
else
puts("Got a valid pointer");
if((len2 = sizeof(p3 = (char *)malloc(0))) == 4)
puts("Got a null pointer");
else
puts("Got a valid pointer");
return 0;
}