int flip(int j,int k,int n) { int t1=(1<<j)-1; int t2=(1<<k)-1; t1=t2^t1; return n^t1; } correct me if im wrong
On Fri, Jun 10, 2011 at 10:09 PM, Kunal Patil <kp101...@gmail.com> wrote: > How about this??? > * > unsigned int flip_j_to_k_bits (unsigned int n,unsigned int j,unsigned int > k) > { > unsigned int temp; > int num_of_on_bits = k-j+1; > > temp = (1<<num_of_on_bits)-1; > temp <<= j; > > return (n^temp); > }* > > I dont know whether shift operation is O(1) or not ! > But i think this is the best possible that can be done !!! > > On Fri, Jun 10, 2011 at 8:40 PM, dinesh bansal <bansal...@gmail.com>wrote: > >> How do you reverse the bits between j to k in a 32 bit integer. >> >> For e.g.: >> >> n = 11100011; j = 2 and k = 5 >> output: 11011111 (bits from 2 to 5 are reversed.) >> >> n = 11010110; j = 1 and k = 5 >> output: 11101000 >> >> O(1) method is preferred. >> Thanks, >> -- >> Dinesh Bansal >> The Law of Win says, "Let's not do it your way or my way; let's do it >> the best way." >> >> -- >> 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. >> >> > -- > 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. > -- 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.