Web Toolbar by Wibiya

Pages

Sunday, June 19, 2011

Write a program to swap odd and even bits of an integer using minimum instructions.


Solution #1: We can mask all the odd bits and shift them by one to right and similarly mask all even bits and shift them by one to the left and then combine the result. Here is the code,

int swapEvenOddBits(int num)
{
return ((num&0xAAAAAAAA)>>1) | ((num&0x55555555)<<1);
}


No comments: