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.

Reply via email to