CSAPP-sol/labs/datalab/README.md
2025-07-22 20:00:27 +08:00

23 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# datalab
## `bitXor`, `tmin`
略。
## `isTmax`
$Tmax$ 的位表示为 `011...11`。注意到 `x+1==~x` 当且仅当 $x=-1$ 或 $x=Tmax$。利用该性质,并判断是否为 $-1$。
## `allOddBits`
每次将 $w$ 位的整数 $x$ 分成 $\frac{w}{2}$ 位的两段,将两者按位与得到新的 $x$,进行该操作 4 次后 $x$ 位数为 2检验第 1 位(从 0 开始编号)是否为 `1`
## `negate`, `isAsciiDigit`
略。
## `conditional`
我们希望设计这样一个函数 $f(x)$:当 $x=0$ 时 $f(x)=0$,而 $x\neq 0$ 时 $f(x)=-1$。此时可令 `conditional(x, y, z) = (y & f(x)) | (z & ~f(x))`
一个简单的设计是 `f(x) = !x - 1`
## `isLessOrEqual`
核心思路是判断 `y - x = y + ~x + 1` 的符号位。需要处理一些细节以规避溢出带来的错误。
## `logicalNeg`
采用 `allOddBits` 方法,略。
## `howManyBits`
对于正数 $x$,所求为最大的 $b$ 使得 $x$ 的第 $b-2$ 位为 `1`,而对于负数 $x$,则是最大的 $b$ 使得 $x$ 的第 $b-2$ 位为 `0`。通过令 `x = x ^ ((x >> 31) << 31)`,我们得以仅用考虑 $x$ 为正数的情况。
之后亦可采用 `allOddBits` 方法,略。
## `floatScale2`,`floatFloat2Int`,`floatPower2`
仔细分类即可。