@Don. Very nice. I think you meant | where you wrote ||. This is so short that you could even do it as a #define:
#define bitExchange(n,i,j) (((((n)>>(i))&1)==(((n)>>(j))&1))?n: (n)^((1<<(i))|(1<<(j)))) Dave On Sep 13, 3:15 pm, Don <[email protected]> wrote: > // If bits i and j are equal, result is just n > // Otherwise, toggle bits i and j > int bitExchange(int n, int i, int j) > { > return (((n>>i)&1) == ((n>>j)&1)) ? n : n ^ ((1<<i) || (1<<j)); > > } > > On Sep 13, 1:50 pm, kumar raja <[email protected]> wrote: > > > > > Suppose a number 'n' is given and two bits positions i,j present in binary > > representation of n . > > > Then how to exchange the contents of the two bits i and j. > > > E.g. n= 13 > > its binary representation is 0000 1101 (just for now consider 8 bit number) > > > i= 2,j=6 > > > o/p : 0100 1001 = 73 > > > please suggest some effective way to do this... > > > -- > > Regards > > Kumar Raja > > M.Tech(SIT) > > IIT Kharagpur, > > [email protected] > > 7797137043. > > 09491690115.- Hide quoted text - > > - Show quoted text - -- 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.
