write a macro definition, the function is to convert the number of an int to the odd and even bits of the interchange, for example, 6 of the binary is 0110, the first and second interchange,// third and fourth position interchange, get 1001, the output should be 9#include <stdio.h >//take out the odd and even digits, the odd digits shift to the left 1 bits, the even digit one bit, add or the phase or # define EXCHANGE (num) ((0x55555555 & num) << 1) + ((0xaaaaaaaa & num) >> 1)) int main () {printf ("%u\n", Exchange (2));p rintf ("%u\n", Exchange (3));p rintf ("%u\n", Exchange (10) );p rintf ("%u\n", EXCHANGE (5)); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C" writes a macro definition, the function of which is to swap the odd and even bits of the number of an int type