"C" writes a macro definition, the function of which is to swap the odd and even bits of the number of an int type

Source: Internet
Author: User
  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

Related Article

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.