⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bits-solve.c

📁 SSD6 pq1的原版正确答案
💻 C
字号:
/*  * bits.c - Solutions to the practical quiz */#include "btest.h"#include <limits.h>/* * Instructions to Students: * * STEP 1: Fill in the following struct with your identifying info. */info_struct info ={   /* Replace with your full name */   "solution",   /* Replace with your login ID */   "solution",};#if 0/* * STEP 2: Read the following instructions carefully. */You will provide your solution to the Data Lab byediting 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 AND HINTS:  1. Each function has a maximum number of operators (! ~ & ^ | + << >>)     that you are allowed to use for your implementation of the function.      The max operator count will be checked by your instructor.      Note that '=' is not counted; you may use as many of these as you      want without penalty.  2. Use the btest test harness to check your functions for correctness.#endif/* * STEP 3: Modify the following functions according the coding rules. *//*  * tmin - return minimum two's complement integer  *   Legal ops: ! ~ & ^ | + << >> *   Max ops: 4 *   Rating: 1 */int tmin(void) {  return 1<<31;}/*  * minusFive - return a value of -5  *   Legal ops: ! ~ & ^ | + << >> *   Max ops: 4 *   Rating: 1 */int minusFive(void) {  return ~(1 << 2);}/*  * 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) {  /* Shift bit to MSB and then right shift (arithmetically) back */  int result = (x<<31)>>31;  return result;}/*  * getBit - Extract bit n from word x *   Bits numbered from 0 (LSB) to 31 (MSB) *   Examples:  *     getBit(0x12345678, 0) = 0 *     getBit(0x12345678, 3) = 1 *   Legal ops: ! ~ & ^ | + << >> *   Max ops: 4 *   Rating: 2 */int getBit(int x, int n) {  int xs = x >> n;   /* Shift n positions right */  return xs & 0x1;   /* Mask low order bit */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -