CS:APP解题记录

CS:APP 2.60

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
unsigned replace_byte(unsigned x, int i, unsigned char b)
{
x = x & (~(0XFF << (i << 3)));//相应字节置零
x = x | (b << (i << 3)); //相应字节改为char b
return x;
}
int main()
{
unsigned ret = replace_byte(0X12345678, 1, 0XAB);
printf("0X%X\n", ret);
return 0;
}

0X1234AB78

利用按位运算$x \& 1 = x , b | 0 = b$。

阅读更多