suppose we have to swap bits  b1 and b2  in a number n.Then

if(b1==b2)  do nothing

else take a hexadecimal number whose all bits are zero except bits b1 and
b2.bit b1=1 and bit b2=1

now  xor of original no with this no will give the desired result.

On Sun, Oct 30, 2011 at 10:32 AM, shiva@Algo <[email protected]> wrote:

> we need to swap only if both the bits are not same
>
> if((n^(1<<i)<n)!=(n^(1<<j)<n))
> n^=(1<<i)+(1<<j);
>
> On 10/29/11, praveen raj <[email protected]> wrote:
> > int func(int x)
> > {
> >     int  y=(1<<i)+(1<<j);
> >      int z=x&y;        // if after bitwise and  ..we get power of 2 then
> ...
> > we have to flip the bits..
> >     if((z&(z-1))==0)
> >        return(x^y);
> >     else
> >       return x;
> > }
> >
> > With regards,
> >
> > Praveen Raj
> > DCE-IT
> > 9999735993
> > [email protected]
> >
> >>
> >>
> >
> > --
> > 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.

Reply via email to