《CSAPP》號稱程序員聖經,雖然中文譯名為《深入理解計算機系統》,但其實沒那么“深”,只是覆蓋面很廣,一般用作計算機專業大一導論課的教科書。早就聽聞書上配套的實驗十分經典,這次重溫新版(第三版),打算把所有的實驗都做一下,也寫個系列博文,好記錄實驗過程。實驗可以在書本配套網站CSAPP: Lab Assignments下載,這篇從第一個實驗 —— 位操作開始。
概述
本實驗是第二章《信息的表示與處理》的配套實驗,要求使用一個高度限制的C語言子集實現一些特定的邏輯,整數,浮點數的函數。延用第一章的說法,信息就是位加上下文,計算機系統中所有的信息都是由一串比特(或者說一串二進制數字)表示的,第二章就講了C語言中整數與浮點數的編碼方式,即典型地,計算機是如何用一串比特來表示整數與浮點數的:
- 無符號整數:直接二進制編碼
- 有符號整數:二進制補碼,最高位為負權
- 浮點數:IEEE 754
同樣從內存里取出4個字節 \(0x80000000\) ,把它當無符號整數看,就是 \(2147483648\);把它當有符號整數看,就是 \(-2147483648\);把它當單精度浮點數看,就是 \(-0\)。所謂上下文,就是解讀這串比特的方式,橫看成嶺側成峰。值得注意的是,盡管在幾乎所有系統上,C語言整數與浮點數都是這么編碼的,但C語言標准本身並沒有這樣規定,不知道有生之年能不能遇上非主流的編碼方式。
如果沒有完全掌握這些數字的編碼方式以及C語言的位操作,是一定無法完成實驗一的。實驗一好就好在會讓你反復回憶這些基礎知識,深入細節之中,做完實驗后想忘都忘不了:)
前提
盡管有C語言有標准,但Undefined Behavior還是太多,尤其是深入底層進行位操作的情況下,因此實驗預設: 有符號整數使用32位二進制補碼編碼; 右移操作為算術位移,高位補符號位。實驗還要求:不能使用宏;整數操作不能使用大於0xFF的常量。下面就逐個函數記錄實驗過程了。
bitAnd
用~
和|
實現&
,有公式很簡單,但記不住,用韋恩圖輔助思考:全集表示所有位都為1,x
與y
分別表示特定位置為1的子集,想象一下~
,|
和&
的韋恩圖,一下子就推出公式來了。
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
return ~(~x | ~y);
}
getByte
x
右移 n * 8
位,取最后一個字節即可,利用了n * 8 == n << 3
。
/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int getByte(int x, int n) {
return (x >> (n << 3)) & 0xFF;
}
logicalShift
實驗預設了右移為算術位移,那么對x
右移n
位再用掩碼將高位補的n
位置0即可。
/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
int mask = ~(((1 << 31) >> n) << 1);
return (x >> n) & mask;
}
bitCount
這題想了很久,正常的想法是將x
一位一位地右移,用掩碼1取最低位,再求和,然而操作符數量超標:D 然后想到,用x & 1
去檢查x
最后一位是否是1比較虧,可以用x & 0x00010001
,這樣可以一次檢查兩位,最后將前后16位的結果匯總即可,然而操作符數量還是超標:D最終將x
分了8組,x & 0x11111111
,每次檢查8位,用了38個操作符,終於達標。這是所有題目中用的操作符數量最多的一題了。
/*
* bitCount - returns count of number of 1's in word
* Examples: bitCount(5) = 2, bitCount(7) = 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 40
* Rating: 4
*/
int bitCount(int x) {
int mask = 0x11 + (0x11 << 8) + (0x11 << 16) + (0x11 << 24);
int count = (x & mask) + ((x >> 1) & mask) +
((x >> 2) & mask) + ((x >> 3) & mask);
return (count & 7) + ((count >> 4) & 7) + ((count >> 8) & 7) +
((count >> 12) & 7) + ((count >> 16) & 7) + ((count >> 20) & 7) +
((count >> 24) & 7) + ((count >> 28) & 7);
}
bang
一開始想在0上面作文章,畢竟只有bang(0) = 1
,但此路不通。|
操作,二分法,逐漸把高位的1收集到低位,如x = x | (x >> 16)
,如果高位的16位有1的話,就會被收集到低位的16位上,依此二分,收集到最后一位,剛好12個操作符。
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12`
* Rating: 4
*/
int bang(int x) {
x = x | (x >> 16);
x = x | (x >> 8);
x = x | (x >> 4);
x = x | (x >> 2);
x = x | (x >> 1);
return ~x & 1;
}
tmin
最簡單的一題,要熟悉二進制補碼。
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return 1 << 31;
}
fitsBits
若x
非負,考慮到n
位二進制補碼能表示的最大非負數為 $0b0111...111 $ (共n-1
個1),用掩碼將x
低位的n-1
位置0,檢查高位的32 - (n - 1)
位是否為0即可。若x
為負,先將其轉為非負數~x
,編碼~x
必需的位數與編碼x
的是相同的。
/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer.
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
int minusOne = ~0;
int mask = minusOne << (n + minusOne);
return !((x ^ (x >> 31)) & mask);
}
divpwr2
x >> n
即為\(\lfloor x / 2^n \rfloor\),結果是向下取整的,但題目要求向0取整,若x
非負向下取整即是向0取整沒有問題,若x
為負,需要向x
加上一個偏移值\(2^n - 1\),使得x >> n
向上取整。
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int divpwr2(int x, int n) {
int signBit = (x >> 31) & 1;
int bias = (signBit << n) + (~signBit + 1);
return (x + bias) >> n;
}
negate
n位二進制補碼的值域是\([-2^{n-1},\ 2^{n-1} - 1]\),並不關於0對稱,因此當x
為最小值時-x
是它自己。
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return ~x + 1;
}
isPositive
正數的符號位為0,0的符號位也是0,是特殊情況。
/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 3
*/
int isPositive(int x) {
return (!!x) & (!(x >> 31));
}
isLessOrEqual
isLessOrEqual
等價於!isGreater
,實現isGreater
簡單點:若x
y
異號,則x
必須非負y
必須為負;若x
y
同號,x - y
不會溢出,必有x - y > 0
,即x - y - 1 >= 0
,即x + ~y >= 0
,檢查x + ~y
的符號位即可。
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int xSign = x >> 31;
int ySign = y >> 31;
int hasSameSign = !(xSign ^ ySign);
int diffSign = (x + ~y) >> 31;
int isXPosYNeg = (!xSign) & ySign;
int isGreater = isXPosYNeg | (hasSameSign & !diffSign);
return !isGreater;
}
ilog2
這道題允許90個操作符,是所有題目對操作符數量最寬松的了。ilog2
的實質是求x
最高位的1的索引,若x
高位的16位有1,則不用管低位的16位;若x
高位的8位有1,則不用管低位的24位,依次類推。實現起來還是十分巧妙的:)
/*
* ilog2 - return floor(log base 2 of x), where x > 0
* Example: ilog2(16) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int ilog2(int x) {
int high16, high8, high4, high2, high1;
high16 = (!!(x >> 16)) << 4;
x = x >> high16;
high8 = (!!(x >> 8)) << 3;
x = x >> high8;
high4 = (!!(x >> 4) << 2);
x = x >> high4;
high2 = (!!(x >> 2) << 1);
x = x >> high2;
high1 = !!(x >> 1);
return high1 + high2 + high4 + high8 + high16;
}
float_neg
終於到浮點數了,浮點數的題對操作符要求寬松一點,還可以用循環跟判斷語句。第一題,只要對IEEE754熟悉就行了。
/*
* float_neg - Return bit-level equivalent of expression -f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, return argument.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf) {
int isNaN = (((uf >> 23) & 0xFF) == 0xFF) && (uf << 9);
return isNaN ? uf : ((1 << 31) ^ uf);
}
float_i2f
沒什么技巧,十分暴力。從符號位,階碼,尾數,舍入,一個一個來。注意,float(x)
是向偶數取整的。
/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
unsigned sign = x & (1 << 31);
unsigned exp = 0;
unsigned frac = 0;
unsigned round = 0;
unsigned absX = sign ? (~x + 1) : x;
unsigned tmp = absX;
while ((tmp = tmp >> 1))
++exp;
frac = absX << (31 - exp) << 1;
round = frac << 23 >> 23;
frac = frac >> 9;
if (round > 0x100) round = 1;
else if (round < 0x100) round = 0;
else round = frac & 1;
return x ? (sign | ((exp + 0x7F) << 23) | frac) + round : 0;
}
float_twice
還是很暴力,按照浮點數分類一個一個來:特殊值,直接返回;規范化的浮點數,階碼加1;非規范化的,左移一位並保持符號位不變。
/*
* float_twice - Return bit-level equivalent of expression 2*f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_twice(unsigned uf) {
unsigned sign = 1 << 31;
unsigned isNormalized = uf << 1 >> 24;
unsigned isSpecial = isNormalized == 0xFF;
if (isSpecial || uf == 0 || uf == sign)
return uf;
if (isNormalized)
return uf + (1 << 23);
return (uf << 1) | (uf & sign);
}