Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Friday, October 20, 2017

Swap bits at even and odd position


Code:

 public static int swapbits(int x)
     {
         int even=x&0xAAAAAAAA; // return set bits at even pos 0xAAAAAAAA is number havin 1 at even pos

         int odd=x&0x55555555; // return set bits at odd pos 0x55555555 is number havin 1 at odd pos

         even>>=1;
         odd<<=1;
         return (odd|even);
     }

0 Comments:

Post a Comment

Stats