Two C bit-operation puzzles.

Discussion in 'C' started by partizan, Feb 18, 2008.

  1. partizan

    partizan New Member

    Joined:
    Feb 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    1. Using only bitwise operations: ! ~ & ^ | + << >> and no loops, function calls, or conditional statements count number of bits in a passed integer. Ex, bitCount(5) = 2, bitCount(7) = 3. Maximum operations allowed is 20.

    2. Using only bitwise operations: ! ~ & ^ | + << >> and no loops, function calls, or conditional statements find sum of three integers using only on + operator. Ex. sum3(3, 4, 5) = 12. Maximum operations allows is 20.

    For both puzzles assume that the size of int is 32bits.

    [​IMG]
     
  2. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    answer of first puzzle.

    int bitCount(unsigned int num)
    {
    unsigned int n;

    n = num - ((num >> 1) & 033333333333) - ((num >> 2) & 011111111111);

    return ((n + (n >> 3)) & 030707070707) % 63;
    }

    You can search on google also.
     
  3. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    2nd puzzle i am not getting!!!

    else

    num1+ num2 + num3
     
  4. partizan

    partizan New Member

    Joined:
    Feb 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I need time to process first solution :)

    Here is clarification on the second one

    static int sum(int x, int y) {
    return x+y;
    }
    int sum3(int x, int y, int z) {
    int word1 = 0;
    int word2 = 0;
    /**************************************************************
    Fill in code below that computes values for word1 and word2
    without using any '+' operations
    ***************************************************************/

    /**************************************************************
    Don't change anything below here
    ***************************************************************/
    return sum(word1,word2);
     
  5. partizan

    partizan New Member

    Joined:
    Feb 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0

    There are some restrictions:
    1. We are not allowed to use unsigned integers
    2. You are using '-' (minus) operator (easy to substitute by [~(x) + 1] )
    3. % as you may see is not allowed

    But thank you for your reply, I will try to see what it does. [​IMG]
     
  6. partizan

    partizan New Member

    Joined:
    Feb 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    sorry there was a typo in the puzzle

    2. Using only bitwise operations: ! ~ & ^ | + << >> and no loops, function calls, or conditional statements find sum of three integers using only one + operator. Ex. sum3(3, 4, 5) = 12. Maximum operations allows is 20.
     
  7. partizan

    partizan New Member

    Joined:
    Feb 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    when you write 033333333333 you mean 0x033.33 right
    and 0x111.11 you mean 0001 0001 ... 0001 in binary. Correct?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice