CareerCup-5.6

Write a program to swap odd and even bits in an integer with as few instructions as possible (e g , bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, etc)

嘿嘿,这个题小Case

#include <iostream>
using namespace std;

int swapBits(int d)
{
    return ((d & 0xAAAAAAAA) >> 1) + ((d & 0x55555555) << 1);
};

int main()
{
    cout<<swapBits(1)<<endl;
    system("pause"); 
};

Comments

comments powered by Disqus