Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *
- * 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 */
- "sreisman@luc.edu+mailpbehrens@gmail.com",
- /* Student name 1: Replace with the full name of first team member */
- "Steve Reisman",
- /* Login ID 1: Replace with the login ID of first team member */
- "sreisman@luc.edu",
- /* The following should only be changed if there are two team members */
- /* Student name 2: Full name of the second team member */
- "Patrick Behrens",
- /* Login ID 2: Login ID of the second team member */
- "mailpbehrens@gmail.com"
- };
- #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) {
- /* to get nor you need to flip the bits of each value and then return the result of and*/
- int and = ~x & ~y;
- √
- return and;
- }
- /*
- * bitXor - x^y using only ~ and &
- * Example: bitXor(4, 5) = 1
- * Legal ops: ~ &
- * Max ops: 14
- * Rating: 2
- */
- int bitXor(int x, int y) {
- int first = x&y;
- int flip = ~first;
- int or = ~x&~y;
- int or2 = ~or;
- int result = flip & or2;
- return result;
- }
- /*
- * 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) {
- int xor = x^y;
- int not1 = !xor;
- int notnot1 = !not1;
- return notnot1;
- }
- /*
- * 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) {
- /*shift the bits of n over to the left by three because there are 4 bytes to deal with*/
- /*take n to the 2^3 and then shift x by n take the two most signifigant bits of x*/
- int temp = n << 3;
- x = x >> temp;
- return x & 0xff;
- }
- /*
- * 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) {
- /*mask x so you get the least signifigant bit*/
- return ((x << 31) >> 31);
- }
- /*
- * TMax - return maximum two's complement integer
- * Legal ops: ! ~ & ^ | + << >>
- * Max ops: 4
- * Rating: 1
- */
- int tmax(void) {
- /*the max two's complement integer is 0 followed by 31 1's*/
- return 0x7FFFFFFF;
- }
- /*
- * 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) {
- // Boolean value indicating sign of x
- // 1 = Negative
- // 0 = Non-Negative
- int sign_x = x >> 31;
- // The negation of the sign bit of value x computes the appropriate boolean return value.
- return (!(sign_x));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement