CSAPP實驗一datalab
其實這個實驗比較難的是bitcount操作,我參考了這個連結http://stackoverflow.com/questions/3815165/how-to-implement-bitcount-using-only-bitwise-operators
* CS:APP Data Lab * * bits.c - Source file with your solutions to the Lab. * This is the file you will hand in to your instructor. * * WARNING: Do not include the <stdio.h> header; it confuses the dlc * compiler. You can still use printf for debugging without including * <stdio.h>, although you might get a compiler warning. In general, * it's not good practice to ignore compiler warnings, but in this * case it's OK. */ #include "btest.h" #include <limits.h> /* * Instructions to Students: * * STEP 1: Fill in the following struct with your identifying info. */ team_struct team = { /* Team name: Replace with either: Your login ID if working as a one person team or, ID1+ID2 where ID1 is the login ID of the first team member and ID2 is the login ID of the second team member */ "1", /* Student name 1: Replace with the full name of first team member */ "", /* Login ID 1: Replace with the login ID of first team member */ "", /* The following should only be changed if there are two team members */ /* Student name 2: Full name of the second team member */ "", /* Login ID 2: Login ID of the second team member */ "" }; #if 0 /* * STEP 2: Read the following instructions carefully. */ You will provide your solution to the Data Lab by editing the collection of functions in this source file. CODING RULES: Replace the "return" statement in each function with one or more lines of C code that implements the function. Your code must conform to the following style: int Funct(arg1, arg2, ...) { /* brief description of how your implementation works */ int var1 = Expr1; ... int varM = ExprM; varJ = ExprJ; ... varN = ExprN; return ExprR; } Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations ! ~ 4. Binary integer operations & ^ | + << >> Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line. You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&, ||, -, or ?: 6. Use any form of casting. You may assume that your machine: 1. Uses 2s complement, 32-bit representations of integers. 2. Performs right shifts arithmetically. 3. Has unpredictable behavior when shifting an integer by more than the word size. EXAMPLES OF ACCEPTABLE CODING STYLE: /* * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31 */ int pow2plus1(int x) { /* exploit ability of shifts to compute powers of 2 */ return (1 << x) + 1; } /* * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31 */ int pow2plus4(int x) { /* exploit ability of shifts to compute powers of 2 */ int result = (1 << x); result += 4; return result; } NOTES: 1. Use the dlc (data lab checker) compiler (described in the handout) to check the legality of your solutions. 2. Each function has a maximum number of operators (! ~ & ^ | + << >>) that you are allowed to use for your implementation of the function. The max operator count is checked by dlc. Note that '=' is not counted; you may use as many of these as you want without penalty. 3. Use the btest test harness to check your functions for correctness. 4. The maximum number of ops for each function is given in the header comment for each function. If there are any inconsistencies between the maximum ops in the writeup and in this file, consider this file the authoritative source. #endif /* * STEP 3: Modify the following functions according the coding rules. * * IMPORTANT. TO AVOID GRADING SURPRISES: * 1. Use the dlc compiler to check that your solutions conform * to the coding rules. * 2. Use the btest test harness to check that your solutions produce * the correct answers. Watch out for corner cases around Tmin and Tmax. */ /* * bitNor - ~(x|y) using only ~ and & * Example: bitNor(0x6, 0x5) = 0xFFFFFFF8 * Legal ops: ~ & * Max ops: 8 * Rating: 1 */ int bitNor(int x, int y) { // ~(x|y) = ~x & ~y return (~x)&(~y); } /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ops: 14 * Rating: 2 */ int bitXor(int x, int y) { //主要使用狄摩根定律 return (~(~x & ~y)) & (~(x & y)); } /* * isNotEqual - return 0 if x == y, and 1 otherwise * Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 6 * Rating: 2 */ int isNotEqual(int x, int y) { return !!(x ^ y); } /* * 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) { //get the mask 0x000000FF int mask = ~((1<<31)>>23); return (x>>(n<<3)) &mask; } /* * copyLSB - set all bits of result to least significant bit of x * Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000 * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */ int copyLSB(int x) { return x<<31>>31; } /* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 1 <= n <= 31 * Examples: logicalShift(0x87654321,4) = 0x08765432 * Legal ops: ~ & ^ | + << >> * Max ops: 16 * Rating: 3 */ int logicalShift(int x, int n) { //int mask = ~((1<<31)>>(n+(~1)+1)); //get the high n bit 0 mask int mask = ~((1<<31)>>n<<1); return (x>>n) & mask; } /* * 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 result; //to get mask1 0x55555555 int tmp_mask1=(0x55)|(0x55<<8); int mask1=(tmp_mask1)|(tmp_mask1<<16); //to get mask2 0x33333333 int tmp_mask2=(0x33)|(0x33<<8); int mask2=(tmp_mask2)|(tmp_mask2<<16); //to get mask3 0x0f0f0f0f int tmp_mask3=(0x0f)|(0x0f<<8); int mask3=(tmp_mask3)|(tmp_mask3<<16); //to get mask4 0x00ff00ff int mask4=(0xff)|(0xff<<16); //to get mask5 0x0000ffff int mask5=(0xff)|(0xff<<8); //add every two bits result=(x&mask1)+((x>>1)&mask1); //add every four bits result=(result&mask2)+((result>>2)&mask2); //add every eight bits result=(result+(result>>4))&mask3; //add every sixteen bits result=(result+(result>>8))&mask4; //add every thirty two bits result=(result+(result>>16))&mask5; return result; } /* * bang - Compute !x without using ! * Examples: bang(3) = 0, bang(0) = 1 * Legal ops: ~ & ^ | + << >> * Max ops: 12 * Rating: 4 */ int bang(int x) { //x or -x when x =0 is 0 while others is 0XFFFFFFF return ~(x |(~x+1))>>31&0x01; } /* * leastBitPos - return a mask that marks the position of the * least significant 1 bit. If x == 0, return 0 * Example: leastBitPos(96) = 0x20 * Legal ops: ! ~ & ^ | + << >> * Max ops: 6 * Rating: 4 */ int leastBitPos(int x) { //x的2complement :the leastbit is invariable. return (~x +1) & x; } /* * TMax - return maximum two's complement integer * Legal ops: ! ~ & ^ | + << >> * Max ops: 4 * Rating: 1 */ int tmax(void) { return ~(1<<31); } /* * isNonNegative - return 1 if x >= 0, return 0 otherwise * Example: isNonNegative(-1) = 0. isNonNegative(0) = 1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 6 * Rating: 3 */ int isNonNegative(int x) { //get the ~x>>31 to get the result return (~x>>31) & 0x01; } /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */ int isGreater(int x, int y) { int signx = x>>31 & 0x01; int signy = y>>31 & 0x01; int sxmy = (x+(~y)+1)>>31 & 0x01; return (((signx ^ 0x01) & (signy ^ 0x01) & (sxmy ) )| (signx & signy & (sxmy )) | ((signx ) &(signy ^ 0x01)) | !(x ^y))^ 0x01 ; } /* * 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 signx = x>>31; //mask = 1<<n -1 int mask = (1<<n) + (~0); int bias = signx & mask; return (x+bias)>>n; } /* * abs - absolute value of x (except returns TMin for TMin) * Example: abs(-1) = 1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 10 * Rating: 4 */ int abs(int x) { //judge the sign ,x>0 =x,x<0: ~x+1 int signx = x>>31; int a = signx ^ x; return a + (~signx + 1); } /* * 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& ~r) & ~(~x&~y& r) // int signx = x>>31 & 0x01; // int signy = y>>31 & 0x01; // int signxy = (x+y)>>31 & 0x01; // return ((signx & signy &(signxy ^ 0x1)) ^ 0x01) & (0x01 ^ ((signx ^ 0x1) & (signy ^ 0x01) & signxy)) ; // // to see the sign of x and y is not both equal to ans int ans = x+ y; return !(( (x ^ans) & (y ^ ans ))>>31); }
相關推薦
CSAPP實驗一datalab
其實這個實驗比較難的是bitcount操作,我參考了這個連結http://stackoverflow.com/questions/3815165/how-to-implement-bitcount-using-only-bitwise-operators *
CSAPP lab1 datalab-handout(深入瞭解計算機系統 實驗一)
能看懂的就不寫註釋了 /* * CS:APP Data Lab * * <Please put your name and userid here> * * bits.c - Source file with your sol
CSAPP深入理解計算機系統實驗一
/* * CS:APP Data Lab * * <Please put your name and userid here> * * bits.c - Source file with your solutions to the Lab.
實驗一、靜態路由實驗
不同 多條 上進 shutdown rate use ima 模擬 掌握 實驗要求1、 掌握路由器的基本配置,幾種工作模式的進入退出方法。2、 掌握靜態路由的作用和配置方法。3、 掌握浮動路由的配置方法。4、 掌握配置靜態負載均衡的方法。5、 掌握路由器上配置遠程登錄的方法
實驗一:實驗環境配置與使用
png 黃色 變化 gdb調試 操作 分析 對比 spa -c 一、實驗目標: 熟悉Linux上C程序的編譯和調試工具,包括以下內容: 1. 了解Linux操作系統及其常用命令 2. 掌握編譯工具gcc的基本用法 3. 掌握使用gdb進行程序調試 二、實驗環境與工件
130242014075-楊利城-實驗一
get 自身 分析師 ora 一定的 整體 行業 bsp 這一 實驗報告 課程 軟件體系結構 實驗名稱 實驗一、軟件設計的網絡支持環境 第 頁 專業____ 軟件工程___________ 班級___ 2班___
130242014008-朱靜如-實驗一
大數據 sites 問題 信息 統一管理 系統 大型 web應用開發 需求 實驗報告 課程 軟件體系結構與設計 實驗名稱 軟件設計的網絡環境 第 頁 專業 軟件工程 班級 1班 學號 130242014008
陳敏敏-130242014024-實驗一
安全瀏覽器 減少 組織 部門 架構 pda ble 總結 結構化 實驗報告一 課程 軟件體系結構與設計 實驗名稱 軟件設計的網絡環境 第 頁 專業 軟件工程 班級 1班 學號 130242014024 姓名 陳敏敏 實驗日期:
130242014050-池國雄-實驗一
再學習 熱門專業 軟件工程 專業技術 ati 定時 實現 公司 企業網 實驗報告 課程軟件體系結構 實驗名稱 實驗一、軟件設計的網絡支持環境 專業____軟件工程_班級___2班______學號__130242014050 姓名池國雄 實驗日期:2017年9月14日報
130242014061-王紹華-實驗一
系統維護 算法 一個 網絡管理 自身 不同 strong 需求分析 自己的 實驗報告 課程 軟件體系結構與設計 實驗名稱 軟件設計的網絡環境 第 1 頁 專業 軟件工程 班級 2班 學號 130242014061 姓名 王紹華 實驗
130242014052-鄒宏亮-實驗一
相關 網站名 部分 處理 國家 參與 廣泛 環境 一半 132042014052-鄒宏亮-實驗一 課程 軟件體系結構 實驗名稱 實驗一、軟件設計的網絡支持環境 第 頁 專業____ 軟件工程___________ 班
130242014026-薛繁雲-實驗一
頻道 技術人 設計師 結果 計算 中國 業務 intern mongodb 實驗報告 課程 軟件系統設計與體系結構 實驗名稱 軟件設計的網絡環境 第 1 頁 專業 軟件工程 班級 1班 學號 130242014026 姓名 薛繁雲
130242014055-陳佳誠-實驗一
方式 是否 我認 不同 什麽是 基本 企業 分析師 under 課程 軟件體系結構與設計 實驗名稱 軟件設計的網絡環境 第 頁 專業 軟件工程 班級 1班 學號 130242014051 姓名 陳達源 實驗日期: 2017 年
130242014060-鄭佳敏-實驗一
分類管理 職位 不同的 item 項目 開發計劃 控制 軟件 lan 軟件體系結構實驗報告 課程: 軟件體系結構 實驗名稱: 軟件設計的網絡支持環境 專業: 軟件工程 學 號 13024201406
130242014059-陳敬龍-實驗一
人工智能 準備工作 body 支持 分離 介紹 控制 int 安全 實驗報告 課程 軟件體系結構與設計 實驗名稱 實驗一:軟件設計的網絡支持環境 專業__軟件工程__ 班級_2班__ 學號_130242014059_ 姓名 陳敬龍 實
實驗一任務四:驗證碼問題
連接 num 轉換 ssa 次循環 random 空字符串 輸出 字符串 package shiyansi; import javax.swing.JOptionPane;public class RandomStr { public static void main(S
cisco綜合配置實驗一
cisco綜合配置實驗實驗目的: 1、把PC1加入VLAN10,PC2J加入VLAN20,PC3加入VLAN30; 2、SW2跟SW3起到路由作用,V10跟VLAN20主要在SW2上運行,VLAN30在SW3上,並且保證SW2跟SW3其中一個壞了,PC1、PC2、PC3仍然可以連接外網; 3
130242014063-李響-實驗一
適用於 得到 是否 項目開發 發展 china style 架構 作者 實驗一 課程 軟件系統設計與體系結構 實驗名稱 軟件設計的網絡支持環境 第 頁 專業___軟件工程_____ 班級_ 2班____ 學號____130242014063_
C++學習,實驗一
space cin 輸出 body .com 找到 string類 指定 中心 一,實驗結論 二,實驗總結和體會1).我在準備實驗過程中,找到了課程公郵-文件中心的進入方法。了解了博客園這個網站。 2).在學習c++第二章的過程中,我發現了以下幾個註意點:
實驗一
alt com ken 一個數 have return nbsp let ++ /*判斷是工作日還是周末*/ #include <iostream> using namespace std; int main(){ int i; cout << "輸