/*** Function: Swap the odd and even bits of an integer, with less instruction the better (that is, bit 0 is exchanged with bit 1, bit 2 is swapped with bit 3 for this column push). */
/** * Idea: first operation of odd digits, and then operation of even digits. Move the odd digits of the number n to the right 1 bits, and the even digits to the left 1 bits. * @param x * @return */public static int swapoddevenbits (int x) {//odd digit right shift, 0XAAAA aaaa=10101010 10101010 10101010 10101010 10 101010 10101010 10101010 10101010b;int odd= (x&0xaaaaaaaa) >>1;//even digit shift left, 0x5555 5555=01010101 01010101 01010101 01010101 01010101 01010101 01010101 01010101b;int even= (x&0x55555555) <<1;return Odd|even;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
9.5-bit operation (vi)--exchanging the odd and even digits of an integer, the less the use of the instruction, the better