Binary/hex, two’s complement, masks, shifts, and overflow patterns 二进制/十六进制、补码、掩码、移位与溢出判定
Week 2 bridges pure C fundamentals (Week 1) and the low-level data manipulation needed for later MIPS, floating-point, and memory manager labs. 第2周连接了第1周的C语言基础与后续MIPS、浮点与内存管理实验所需的底层数据操作能力。
Use the flow below to stay exam-ready while keeping practice synced with the tutorials and labs. 按照下列节奏推进,可在同步完成教程/实验的同时保持面向考试的准备状态。
Aim for ≥4 conversion tasks (binary↔decimal↔hex), ≥3 mask manipulations, ≥2 overflow diagnoses, and ≥1 full lab function implementation each week. 每周至少完成4次进制转换(十进制/二进制/十六进制互换)、3次掩码操作、2次溢出判定、1个完整实验函数实现。
Complete official tests, then add custom inputs that hit corner cases (e.g., turning high bits on/off, rotating 0 or all-ones). 在完成官方测试后,再添加高位开关、旋转0或全1等边界输入自行验证。
Draw a ladder: decimal ↔ binary ↔ hex. Convert through binary to avoid mistakes. 画出“十进制↔二进制↔十六进制”阶梯,始终通过二进制中转以减少错误。
Checklist: same sign inputs? Does the sign of the result flip? If yes, overflow occurred. 检查表:输入是否同号?结果符号是否翻转?若是,则发生溢出。
1) Shift target field down; 2) Apply mask (width bits of 1); 3) Optionally sign-extend if the field is signed. 1)右移到最低位;2)以字段宽度构建全1掩码;3)若字段有符号则做符号扩展。
Left shift by k ≈ multiply by 2^k (beware overflow). Logical right ≈ unsigned divide by 2^k; arithmetic right ≈ signed divide rounding toward -∞. 左移k位≈乘以2^k(注意溢出);逻辑右移≈无符号除以2^k;算术右移≈向下取整的有符号除以2^k。
8-bit unsigned: 0..255; 8-bit two’s complement: -128..127. Hex groups 4 bits per digit for readability. 8位无符号:0..255;8位补码:-128..127。十六进制每位代表4个二进制位,便于阅读。
Negate by invert bits + 1. Add/subtract works uniformly for signed and unsigned, but interpretation differs. 取负:按位取反再加1。加/减运算对有符号与无符号统一实现,但数值解释不同。
Logical right (>> on unsigned) fills with 0s; arithmetic right (>> on signed) preserves sign bit. 无符号右移用0填充;有符号右移保留符号位(算术右移)。
Byte ordering (little vs big-endian). Useful context; rarely examined standalone. 字节顺序(小端/大端)。作为背景知识较有用,但单独考查较少。
Q: Encode -37 in 8-bit two’s complement. 问:将 -37 表示为 8 位补码。
Q: Extract bits [15:8] from a 32-bit value x. 问:从32位整数 x 中抽取 [15:8] 位。
Q: What is (unsigned)0xF0 >> 2 and why? What about (signed)-8 >> 2? 问:(unsigned)0xF0 >> 2 的结果与原因?(signed)-8 >> 2 又是多少?
Exam-style prompt: “Given two signed 16-bit integers a and b, produce their sum unless overflow occurs. If overflow, clamp to INT16_MAX or INT16_MIN.” 考试风格题: “给定两个16位有符号整数 a 与 b,若相加溢出则输出 INT16_MAX/INT16_MIN,否则返回和。”
int32_t s = (int32_t)a + (int32_t)b;
使用32位临时变量求和:int32_t s = (int32_t)a + (int32_t)b;
((a ^ s) & (b ^ s)) & 0x8000 is non-zero.
通过符号位检测溢出:若 ((a ^ s) & (b ^ s)) & 0x8000 ≠ 0,则溢出。
s when safe. When overflow occurs, choose clamp using conditional operator.
无溢出时返回 s;若溢出,则根据符号返回相应的饱和值。
Tie back to templates: use Template 2 for overflow logic, Template 3 for mask application. 与模板回链:溢出判断使用模板2,掩码操作利用模板3思路。
Prompt: “Given four 1-bit boolean flags (north, south, east, west) and a 4-bit error code, compose a status word: bits [3:0] store error, bits [7:4] store flags (N,S,E,W).” 题目: “给定四个1比特布尔标记(north/south/east/west)和4位错误码,将它们打包成状态字:低4位为错误码,高4位依次为N、S、E、W。”
Reverse operation: apply Template 3 to read back each flag with mask+shift in quizzes. 逆操作:在测验中可用模板3通过掩码+移位读取每个标志位。
Implement: count_ones(x), set_field(x,h,l,val), clear_lowest_set(x). 实现:count_ones(x)、set_field(x,h,l,val)、clear_lowest_set(x)。
Write a small command-line tool that reads integers until EOF and reports population count, parity (odd/even number of 1s), and highest set bit position. 编写命令行小工具,读取整数直到EOF,并输出1的数量、奇偶校验(1的个数奇偶)以及最高位1的位置。
Reuse count_ones and clear_lowest_set, and log tricky inputs (0, power-of-two, alternating bits) for quiz revision.
复用 count_ones 与 clear_lowest_set,将棘手输入(0、2的幂、交替比特)记录下来备测验复习。