Thread: C bitwise operator exercise - more like newb killer

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    Question C bitwise operator exercise - more like newb killer

    I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my hair....especially ANSI C's bitwise operators.....
    For reference i come from the (Java/C#/C++) realm and was never forced to use these.

    Many people dont understand these....I tried to make sense....I know the truth tables...and I can do simple operation....can anyone provide a couple answers and possibly give me some explainations on how to attack these...any special tricks on attacking these....I am familiar with these operators but these are tricky and none of my friends got these....I am the most advanced since I at least got 3 correctly..........

    Any help is much appreciated.
    Here is the homework.....

    Code:
    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;
      }
    
    #endif
    
    /*
     * 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) {
    /* Complementing x and y(mirroring bits)
     * and then applying the and operator
     */
      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) {
    
    
    
    /* Using the slides on demorgans laws
     * I combined 2 equalies to recieve
     * this result
     * The complement of the complement
     * of the complement of x AND y 
     * and the complement x and the complement of
     * y
     */
    
      return ( ~ ( ~(~x&y) & ~ (x&~y)    ));
    
    }
    /* 
     * evenBits - return word with all even-numbered bits set to 1
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 8
     *   Rating: 2
     */
    int evenBits(void) {
     
    
      return 2;
    
      
    }
    /* 
     * 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) {
     
    
     return 2 ;
    }
    /* 
     * 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) {
      return 2;
    }
    /* 
     * bitMask - Generate a mask consisting of all 1's 
     *   lowbit and highbit
     *   Examples: bitMask(5,3) = 0x38
     *   Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
     *   If lowbit > highbit, then mask should be all 0's
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 16
     *   Rating: 3
     */
    int bitMask(int highbit, int lowbit) {
      return 2;
    }
    /* 
     * TMax - return maximum two's complement integer 
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 4
     *   Rating: 1
     */
    int tmax(void) {
     
      return ~(0x08<<28);
    }
    /* 
     * 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) {
        return 2;
    }
    /* 
     * isLess - if x < y  then return 1, else return 0 
     *   Example: isLess(4,5) = 1.
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 24
     *   Rating: 3
     */
    int isLess(int x, int y) {
           return 2;
    }
    /* 
     * 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) {
     
      return ((x>>31)+1);
    }
    /* 
     * 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) {
      return 2;
    }
    /*
     * multFiveEights - multiplies by 5/8 rounding toward 0.
     *   Examples: multFiveEights(77) = 48
     *             multFiveEights(-22) = -13
     *   You can assume |x| < (1 << 29)
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 12
     *   Rating: 3
     */
    int multFiveEights(int x) {
      return 2;
    }
    /* 
     * 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) {
      return 2;
    }
    /* 
     * bang - Compute !x without using !
     *   Examples: bang(3) = 0, bang(0) = 1
     *   Legal ops: ~ & ^ | + << >>
     *   Max ops: 12
     *   Rating: 4 
     */
    int bang(int x) {
      return 2;
    }
    /* 
     * sm2tc - Convert from sign-magnitude to two's complement
     *   where the MSB is the sign bit
     *   Example: sm2tc(0x80000005) = -5.
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 15
     *   Rating: 4
     */
    int sm2tc(int x) {
      return 2;
    }
    Any help is much appreciated
    Last edited by shdwsclan; 02-22-2006 at 12:05 AM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    Yes, not only that, I have a book and I do get the gist of how they work and truth table, ive also updated my code with the isNonNegative function...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, most of those are pretty easy, where are your attempts at your homework?

    Post some attempts, and we'll tell you if you're on the right track.

    I mean, things like getByte should be a doddle for anyone whose "the most advanced since I at least got 3 correctly"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 06:45 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. bitwise negation
    By Sathyabodh in forum C Programming
    Replies: 1
    Last Post: 08-12-2003, 09:42 AM
  4. Characters into bitwise ints
    By Code Zer0 in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2003, 08:34 AM
  5. Replies: 5
    Last Post: 10-30-2002, 10:23 PM