9.5-bit operation (vi)--exchanging the odd and even digits of an integer, the less the use of the instruction, the better

Source: Internet
Author: User

/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.