CSAPP 實驗一 位運算

2021-07-08 15:26:50 字數 3162 閱讀 2957

課程鏈結如下:coursera–hardware/software inte***ce

這門課的大部分內容是對應於書《深入理解計算機系統》,該書的英文名稱是《computer systems: a programmer』s perspective》。 該書講解了計算機的底層知識,其中大部分我認為是乙個合格程式設計師必須掌握的,在此推薦此書。該課的主要亮點是實驗,5個實驗不同的知識考查,能夠讓我們比平時更加深入地了解計算機的原理。

/* 

* bitand - x&y using only ~ and |

* example: bitand(6, 5) = 4

* legal ops: ~ |

* max ops: 8

* rating: 1

*/int bitand(int x, int 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)

/* * 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)

// 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)

/* * 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)

/* * 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)

// 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)

/* * 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)

// 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)

// 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)

// 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)

C語言基礎一 位運算

程式是按照從上到下的順序依次執行的。選擇結構,程式中會判斷並選擇一條符合條件的語句執行。if 條件表示式 執行的語句 else if 條件表示式 執行的語句 else 執行的語句 switch 表示式 不能為小數,因為計算機中的小數只能儲存近似值。break 是跳出這個選擇結構,若是沒有break,...

一 位運算的奇巧淫技

補充 異或,可以理解為不進製加法 1 1 0,0 0 0,1 0 1 性質 1 交換律,可任意交換運算因子的位置,結果不變 2 結合律 即 a b c a b c 3 對於任何數x,都有x x 0,x 0 x 4 自反性a b b a 0 a,連續和同乙個因子做異或運算,最終結果為自己 題1 找出唯...

判定字元是否唯一 位運算

實現乙個演算法,確定乙個字串s的所有字元是否全都不同。程式設計師面試金典 01.02 這是乙個簡單的問題,解決的辦法比較多,比如雙迴圈呀,利用c 的stl呀,或者使用各種標誌容器記錄呀,這裡給出一種標誌容器的方法 bool isunique string astr return true 時間的維度...