Solution
#include <iostream>
using namespace std;
unsigned int swap_bits(int a)
{
unsigned int b = a;
unsigned int c = b & 0xAAAAAAAA;
unsigned int d = b & 0x55555555;
c >>= 1;
d <<= 1;
return (unsigned int)( c | d);
}
int main(int argc, char* argv[])
{
cout.unsetf(ios::dec);
cout.setf(ios::hex);
cout.setf(ios::showbase);
unsigned int a = 0xAAAA;
cout << a << endl;
cout << swap_bits(a) << endl;
return 0;
}
Problem
Write a program to swap odd and even bits in an integer