unsigned int reverse_bits (unsigned int n) { if(n==1) return n; int j,p; for(j = 31;j>0;j--) if(n&(1<<j)) break; //to find the first set bit from left p =0; while(j>p) { int x1 = (n& (1<<j))>>j; int x2 = (n&(1<<p))>>p; if(x1!=x2) { if(x1) { n &= ~(1<<j);//unset the jth bit n |= (1<<p); //set the pth bit } else { n &= ~(1<<p); //unset the pth bit n |= (1<<j); //set the jth bit } } j--;p++; } return n; }
On 7/21/11, archita <kool.arc...@gmail.com> wrote: > How to reverse the order of bits of a number in minimum space > complexity? > if the no is 11-1011 > the output should b 1101. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- *Piyush Sinha* *IIIT, Allahabad* *+91-7483122727* * <https://www.facebook.com/profile.php?id=100000655377926> "NEVER SAY NEVER" * -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.