Professional Documents
Culture Documents
作业4 CSAPP Chap 2
作业4 CSAPP Chap 2
作业4 CSAPP Chap 2
CS:APP Chap. 2
2.14
expression bin hex
x 011001100x66
y 001110010x39
x&y 00100000 0x20
x|y 01111111 0x7F
~x | ~y 11011111 0xDF
x & !y 00000000 0x00
x && y 00000001 0x01
x || y 00000001 0x01
!x || !y 00000000 0x00
x && ~y 00000001 0x01
2.16
x x<<3 x>>2(logical) x>>2(arithmetic)
hex bin bin hex bin hex bin hex
0xC3 11000011000110000x18 001100000x30 11110000 0xF0
0x75 0111010110101000 0xA8 000111010x1D 000111010x1D
0x87 10000111001110000x38 00100001 0x21 111000010xE1
0x66 01100110001100000x30 000110010x19 000110010x19
2.23
A.
w fun1(w) fun2(w)
0x00000076 0x00000076 0x00000076
0x87654321 0x00000021 0x00000021
0x000000C9 0x000000C9 0xFFFFFFC9
0xEDCBA987 0x00000087 0xFFFFFF87
B.
fun1(w):相当于 w%256。
fun2(w) : 若 0≤w%256≤127 , 返 回 w ; 若 127≤w%256≤255 , 返 回 w-128 。 或 者 相 当 于
(w+128)%256-128。
2.26
A. 当 strlen(s)<strnlen(t)时,函数将返回 1,而正确结果是 0。
B. strlen 具有 size_t 返回类型,strlen(s)-strlen(t)也将是 unsinged int,strlen(s)<strlen(t)时表达
式的实际值为 strlen(s)-strlen(t)+UMax+1,大于 0。
C. 改为 return strlen(s)>strlen(t)。
2.35
1) x ⋅ y 的 位 向 量 截 断 至 后 32 位 , 视 为 无 符 号 数 u, 截 断 的 高 位 为 有 符 号 数 v , 则
。于是 或 , , w。
w
x ⋅ y=v 2 +u p=U 2 T ( u ) u=T 2 U ( p ) u=p+ p w−1 2w x ⋅ y= p+ ( pw−1 + v ) 2
2.47
Bits e E 2E f M 2E×M V Decimal
0 00 00 0 0 1 0/4 0/4 0 0 0
0 00 01 0 0 1 1/4 1/4 1/4 1/4 0.25
0 00 10 0 0 1 2/4 2/4 2/4 1/2 0.5
0 00 11 0 0 1 3/4 3/4 3/4 3/4 0.75
0 01 00 1 0 1 0/4 4/4 1 1 1
0 01 01 1 0 1 1/4 5/4 5/4 5/4 1.25
0 01 10 1 0 1 2/4 6/4 6/4 3/2 1.5
0 01 11 1 0 1 3/4 7/4 7/4 7/4 1.75
0 10 00 2 1 2 0/4 4/4 2 2 2
0 10 01 2 1 2 1/4 5/4 10/4 5/2 2.5
0 10 10 2 1 2 2/4 6/4 12/4 3 3
0 10 11 2 1 2 3/4 7/4 14/4 7/2 3.5
0 11 00 - - - - - - +inf -
0 11 01 - - - - - - NaN -
0 11 10 - - - - - - NaN -
0 11 11 - - - - - - NaN -
2.61
A. !~x
B. !x
C. !~((x << 24) >> 24)
D. !((x >> 24) << 24)
2.65
int odd_ones(unsigned x){
x = (x >> 16) ^ (x & 0xFF); // bitwise XOR. Two 1s or two 0s produce 0.
// if there were odd 1s in x, now there are odd 1s in new x[15:0]
x = (x >> 8) ^ (x & 0xF);
x = (x >> 4) ^ (x & 7);
x = (x >> 2) ^ (x & 3);
x = (x >> 1) ^ (x & 1);
return x;
}
2.69
unsigned rotate_left(unsigned x, int n){
return (x << n) | (x >> (w - n))
// (x << n) has n 0s on the right, preserving the (w - n) lower bits on the left
// while (x >> (w - n)) has (w – n) 0s on the left, preserving n higher bits on the right
// combine the two bit patterns by bitwise OR
}
2.75
令有符号数 和 的位向量对应的无符号数分别为 和 。 , ,
x y u v u=x+ x w−1 2w v= y + y w−1 2 w
因此实数乘积
其中
T 2 U ( x ¿tw y )=u ¿ uw v 是w 位的值。所以
取uv 的高w 位为
2.96
int float_f2i(float_bits f)
{
uint32_t mantissa = (f << 9 >> 9) | (1 << 23); // put 24 bits on the right
int expo = ((f >> 23) & 0xFF) - 127;
int left_shift = expo - 23; // if expo==23, rounded=mantissa
uint32_t rounded;
if (left_shift >= 8)
rounded = 1 << 31; // overflow
else if (left_shift >= 0)
rounded = mantissa << left_shift;
else if (left_shift >= -24)
rounded = mantissa >> (-left_shift);
else
rounded = 0;
if (f >> 31) // check sign
rounded = -rounded;
return rounded;
}