reverse bits

Write an Efficient C Program to Reverse Bits of a Number ??

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. some crazy things are happening here...i tried to post my solution two times...both the time the code that got published was completely different from what i wrote.
    I had to delete the solution therefore...its happening again and again...its not letting me to post the solution...there is some serious bug.

    ReplyDelete
  4. int a=0,b=31;
    while(a<b){
    if(((n<<a)&1)^((n<<b)&1)){
    n^=1<<a;
    n^=1<<b;
    }
    a++;
    b--;
    }

    ReplyDelete
  5. hmmmmm...finally...the above code is for any 32 bit integer n.

    I am unsure if i hv to transform 00...010111 to 00....011101 or 111010...00. The above code is for the latter case.

    ReplyDelete
  6. sorry the inner while loop condition is
    ((n>>a)&1)^((n>>b)&1))

    ReplyDelete
  7. @jainendra i think you have posted in some different post..kindly check..:)

    ReplyDelete
  8. this code is for reversing individual bits of a number.you have to reverse entire number.
    eg: 00000101 -> 10100000

    ReplyDelete
  9. you can check this code
    For 32-bit integers:
    x = ((x >> 16) & 0X0000FFFF) | ((x & 0X0000FFFF) << 16);
    x = ((x >> 8) & 0X00FF00FF) | ((x & 0X00FF00FF) << 8);
    x = ((x >> 4) & 0X0F0F0F0F) | ((x & 0X0F0F0F0F) << 4);
    x = ((x >> 2) & 0X33333333) | ((x & 0X33333333) << 2);
    x = ((x >> 1) & 0X55555555) | ((x & 0X55555555) << 1);
    x is now the binary reversal of its original value.

    ReplyDelete

Post a Comment

Popular posts from this blog