Thread: Bit manipulation

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Bit manipulation

    I am getting owned by this bit manipulation assignment. I understand how all of the operators work, and have a general grasp of when to use each of them, but i feel there is something big that I'm missing. It took me long torturous hours to figure out the answer to #1 (isTmax()), and I don't even think I did it the way the instructor wanted me to. Any advice on how to go about these???????

    Code:
    /*  * CS:APP Data Lab 
     * 
     * <Please put your name and userid here>
     * 
     * 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.  
     */
    
    
    #if 0
    /*
     * Instructions to Students:
     *
     * STEP 1: Read the following instructions carefully.
     */
    
    
    You will provide your solution to the Data Lab by
    editing the collection of functions in this source file.
    
    
    INTEGER 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.
      7. Use any data type other than int.  This implies that you
         cannot use arrays, structs, or unions.
    
    
     
      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;
      }
    
    
    FLOATING POINT CODING RULES
    
    
    For the problems that require you to implent floating-point operations,
    the coding rules are less strict.  You are allowed to use looping and
    conditional control.  You are allowed to use both ints and unsigneds.
    You can use arbitrary integer and unsigned constants.
    
    
    You are expressly forbidden to:
      1. Define or use any macros.
      2. Define any additional functions in this file.
      3. Call any functions.
      4. Use any form of casting.
      5. Use any data type other than int or unsigned.  This means that you
         cannot use arrays, structs, or unions.
      6. Use any floating point data types, operations, or constants.
    
    
    
    
    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. Use the BDD checker to formally verify your functions
      5. 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.
    
    
    /*
     * STEP 2: 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 BDD checker to formally verify that your solutions produce 
     *      the correct answers.
     */
    
    
    
    
    #endif
    /*
     * isTmax - returns 1 if x is the maximum, two's complement number,
     *     and 0 otherwise 
     *   Legal ops: ! ~ & ^ | +
     *   Max ops: 10
     *   Rating: 1
     */
    int isTmax(int x) {
      int y = x+1; //add one to x and save as y to get 0xF000000 on desired input
      int outlier = !(y); //set to 1 if input is -1, else 0
      x = x+y; //add to x so desired input becomes 0xFFFFFFFF
      return !((~x)|outlier); //flip and ! x, and check to make sure input is not -1
    }
    
    
    /* 
     * upperBits - pads n upper bits with 1's
     *  You may assume 0 <= n <= 32
     *  Example: upperBits(4) = 0xF0000000
     *  Legal ops: ! ~ & ^ | + << >>
     *  Max ops: 10
     *  Rating: 1
     */
    int upperBits(int n) {
      int start = !!n;
      start = start<<31;
      start = start>>n;
      start = start<<1;
      return start;
      
    
    
    }
    /* 
     * allEvenBits - return 1 if all even-numbered bits in word set to 1
     *   Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 12
     *   Rating: 2
     */
    int allEvenBits(int x) {
      
      return 2;
    }
    /*
     * ezThreeFourths - multiplies by 3/4 rounding toward 0,
     *   Should exactly duplicate effect of C expression (x*3/4),
     *   including overflow behavior.
     *   Examples: ezThreeFourths(11) = 8
     *             ezThreeFourths(-9) = -6
     *             ezThreeFourths(1073741824) = -268435456 (overflow)
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 12
     *   Rating: 3
     */
    int ezThreeFourths(int x) {
      return 2;
      
    }
    /* 
     * replaceByte(x,n,c) - Replace byte n in x with c
     *   Bytes numbered from 0 (LSB) to 3 (MSB)
     *   Examples: replaceByte(0x12345678,1,0xab) = 0x1234ab78
     *   You can assume 0 <= n <= 3 and 0 <= c <= 255
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 10
     *   Rating: 3
     */
    int replaceByte(int x, int n, int c) {
     return 2;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to actually make some effort, and post what you are having trouble with.
    Code:
    int ezThreeFourths(int x) {
      return five ops;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Quote Originally Posted by quzah View Post
    You need to actually make some effort, and post what you are having trouble with.
    Code:
    int ezThreeFourths(int x) {
      return five ops;
    }

    Quzah.
    really? I solved the first puzzle already dumbass. I also posted what my problem was. I don't know how I am supposed to solve them properly. You're a big tool kid

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You didn't post what you were having trouble with. You didn't say why exactly you can't figure out how to multiply by three and divide by four, did you? That's pretty basic stuff.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Works fine for me. The function returns 1 for input 2147483647 (maximum signed integer) and 0 otherwise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit manipulation
    By dsupriya in forum C Programming
    Replies: 8
    Last Post: 03-05-2009, 09:36 AM
  2. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  3. Bit Manipulation???
    By John_L in forum C Programming
    Replies: 1
    Last Post: 11-28-2007, 01:06 AM
  4. Bit manipulation
    By crag2804 in forum C Programming
    Replies: 3
    Last Post: 09-29-2002, 06:33 PM
  5. Help - Bit Manipulation
    By LivLuvLafLrn in forum C Programming
    Replies: 5
    Last Post: 06-06-2002, 02:47 PM