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:
Post a Comment