/*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)
*/
#include <iostream>
#include <bitset>
using namespace std;
bitset<32> swapoddeven(int x){
int k =(((x & 0xaaaaaaaa)>>1) | ((x & 0x55555555)<<1));
bitset<32> bi(k);
return bi;
}
int main(){
int x = 43;
bitset<32> b(x);
cout<<b<<endl;
cout<<swapoddeven(x)<<endl;
}
careercup5.6
最新推荐文章于 2018-04-04 17:51:00 发布