CSAPP第二章家庭作业2.59

题目

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

//返回x的最低有效字节和y剩下的字节
unsigned int cal(unsigned int x, unsigned int y) {
unsigned int t = 0xFF;
unsigned int res = (x & t) | (y & ~t);
printf("%x\n", res);
return res;
}

int main() {
unsigned int x = 0x89ABCDEF;
unsigned int y = 0x76543210;
//预期结果:z
unsigned int z = 0x765432EF;
printf("%d\n", z == cal(x, y));
return 0;
}

总结

采用掩码。