首页 > 代码库 > 二进制打印与逆序_C语言(转)

二进制打印与逆序_C语言(转)

//二进制逆序 by MoreWindows( http://blog.csdn.net/MoreWindows )  #include <stdio.h>//二进制打印函数template <class T>void PrintfBinary(T a){    int i;    for (i = sizeof(a) * 8 - 1; i >= 0; --i)    {        if ((a >> i) & 1)            putchar(1);        else             putchar(0);        if (i == 8)            putchar( );    }    putchar(\n);}int main(){    printf("二进制逆序 \n\n");    printf("逆序前:    ");    unsigned short a = 0x1FFF;    PrintfBinary(a);    a = ((a & 0xAAAA) >> 1) | ((a & 0x5555) << 1);    a = ((a & 0xCCCC) >> 2) | ((a & 0x3333) << 2);    a = ((a & 0xF0F0) >> 4) | ((a & 0x0F0F) << 4);    a = ((a & 0xFF00) >> 8) | ((a & 0x00FF) << 8);    printf("逆序后:    ");    PrintfBinary(a);    printf("false   ");}

转自:http://blog.csdn.net/MoreWindows

技术分享

二进制打印与逆序_C语言(转)