setbits

This is a discussion on setbits within the C Programming forums, part of the General Programming Boards category; Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    setbits

    Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

    Code:
    unsigned setbits(unsigned x, int p, int n, unsigned y)
    {
      return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));
    }
    Can someone please explain me how does this work:

    x & ((~0 <<(p+1))

    p = 5;
    char x = 0011 0100

    0011 0100 &
    0011 1111 // this set of bits?
    ---------------
    0011 0100

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a FAQ on bit manipulation that covers each of the operations.

    ~ is to flip the bits the other way. So ~0 is setting all bits on.
    << shift to the left p+1 times
    & what you have shown

    What part aren't you understanding? (hint: read the FAQ)

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

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    edit: its ok, i understand everything now!
    Last edited by Tool; 12-17-2009 at 03:38 AM.

  4. #4
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17

    Brackets

    Please check the brackets properly, I think you are calculating it wrongly

    Code:
    return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));

  5. #5
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17
    and in the first block also,
    Code:
    (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n)))))

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Thx, i got it now .

  7. #7
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    What about the conditions...

    This is the function i wrote, but what should i do in case x receives a negative value? Should i rather declare setbits as int and then check if x is negative? Seems like the program automaticaly turns it into a positive value if its negative, and thats not good...

    Code:
    unsigned setbits(unsigned int x, int p, int n, unsigned int y)
    {
       if(n < 1 || n>p-1  
          || p < 0 || p>sizeof(unsigned)*8) 
       return 0;
       
       unsigned tmp1 = x&(~((~(~0<<n))<<p+1-n));
       unsigned tmp2 = ((y&(~(~0<<n))))<<p+1-n;
       return tmp1 | tmp2;
    }

  8. #8
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    944
    x is unsigned, how is it going to be negative?

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    int main {
    
    setbits(-250, 5, 3, 15);
    
    return 0;
    }
    My compiler actually compiles this, thats why i find it strange.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Turn up your compiler warning level then.

    Note that the solution you're working on looks a fair bit more complicated than it could be.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    >Note that the solution you're working on looks a fair bit more complicated than it could be.

    It does the job. I couldve written those last 3 lines in 1, but i preffer not to.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    16

    Nice explanation of this on my recent post

    Kernighan and Ritchie confusion

    I was just doing this exercise and needed it explaining, which someone did very well!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise setbits function (knr 2-6)
    By olbas in forum C Programming
    Replies: 17
    Last Post: 03-10-2010, 06:40 AM
  2. [C] Setbits function
    By Tool in forum C Programming
    Replies: 23
    Last Post: 07-10-2009, 12:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21