@balaji: right , just one change required i think so coz in question they are asking for change of one more bit i.e. for j=2,k=5.. bits 2,3,4,5 are modified..ur code is doing i guess only 2,3,4.. i think just one change needed int t2=(1<<(k+1))-1;
On Fri, Jun 10, 2011 at 10:22 PM, Vetri Balaji <[email protected]>wrote: > 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 <[email protected]> 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 <[email protected]>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 [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> 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 [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
