最近學習了Coursera上面的《Hardware/Software Interface》 。該課源自於華盛頓大學計算機專業,華盛頓大學將該課搬到了Coursera,現在已經是公開課性質的,視頻完全可以自由下載。
課程鏈接如下:Coursera–Hardware/Software Interface
這門課的大部分內容是對應於書《深入理解計算機系統》,該書的英文名稱是《Computer Systems: A Programmer’s Perspective》。 該書講解了計算機的底層知識,其中大部分我認爲是一個合格程序員必須掌握的,在此推薦此書。該課的主要亮點是實驗,5個實驗不同的知識考查,能夠讓我們比平時更加深入地瞭解計算機的原理。
位運算
實驗一考查了位運算,實驗的要求是實現用幾個有限的運算符實現簡單的功能函數。但是有一些自己感覺還是很難實現,參考了github之後才明白過來。具體如下:
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
//只用 ~ 和 | 操作符 來實現與操作
// 將 x 和 y的 爲0的位 並起來 然後取反即可
return ~(~x|~y);
}
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 14
* Rating: 1
*/
int bitXor(int x, int y) {
//要點:找到 x 和 y 中 都爲0 和都爲1的 位
// (x&y)都爲1 (~x&~y)都爲0
return ~(x&y)&(~(~x&~y));
}
/*
* thirdBits - return word with every third bit (starting from the LSB) set to 1
* and the rest set to 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 1
*/
int thirdBits(void) {
//返回的int值的二進制中每3位中有一個1並在3位中的最低位
int result = 0x49;
result = (result << 8) + 0x24;
result = (result << 8) + 0x92;
result = (result << 8) + 0x49;
return result;
}
// Rating: 2
/*
* 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 mask = x >> 31;
return !(((~x & mask) + (x & ~mask)) >> (n + ~0));
}
/*
* sign - return 1 if positive, 0 if zero, and -1 if negative
* Examples: sign(130) = 1
* sign(-23) = -1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 10
* Rating: 2
*/
int sign(int x) {
//判斷一個數爲正負或者0
int s = x >> 31;
int sig = (s & (~0)) + ((!s) & (!!x));
return sig;
}
/*
* 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) {
//返回x中的第N字節
return (x>>(n<<3))&(0x000000ff);
}
// Rating: 3
/*
* 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;
}
/*
* addOK - Determine if can compute x+y without overflow
* Example: addOK(0x80000000,0x80000000) = 0,
* addOK(0x80000000,0x70000000) = 1,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int addOK(int x, int y) {
//要點:相加前符號位相同,相加後的結果符號位與x和y不同,anemic代表加法溢出
int sx = x>>31;
int sy = y>>31;
int ssum = (x+y) >> 31;
return !(~(sx ^ sy) & (sy ^ ssum));
}
// Rating: 4
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return (~((x >> 31) | (((~x) + 1) >> 31))) & 1;
}
// Extra Credit: Rating: 3
/*
* conditional - same as x ? y : z
* Example: conditional(2,4,5) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int conditional(int x, int y, int z) {
return (((!!x << 31) >> 31) & y) +((((!x) << 31) >> 31) & z);
}
// Extra Credit: Rating: 4
/*
* isPower2 - returns 1 if x is a power of 2, and 0 otherwise
* Examples: isPower2(5) = 0, isPower2(8) = 1, isPower2(0) = 0
* Note that no negative number is a power of 2.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 4
*/
int isPower2(int x) {
//判斷是否爲2的冪
//要點:x+mask 爲 x-1 x&(x-1) 相當於得到x去掉了最低的爲1的那位後的數,2的次冪數對應2進制應該只有1位1 同時還要保證x不爲0和負數
int mask = ~0;
return (!(x & (x + mask)) & (!(x >> 31)) & (~(!x)));
}