标签:
//用宏实现一个整形奇偶位交换 //例如6为0000..0110,交换完为0000..1001,即为9 #include <stdio.h> #define EXCHANGE(num) ((num&(0x55555555))<<1)|((num&(0xaaaaaaaa))>>1) //取出奇数位左移,取出偶数位右移,最后相加 int main() { printf("%d\n", EXCHANGE(6)); //9 printf("%d\n", EXCHANGE(0)); //0 printf("%d\n", EXCHANGE(-1)); //-1 printf("%d\n", EXCHANGE(-2)); //-3 return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/doudouwa1234/article/details/46706701