任意输入四位十六进制整数,以反序方式输出
#include <stdio.h> void fun1(unsigned short a) { unsigned short b; printf("======== "); printf("%#X ", (unsigned short) (a << 12) >> 12 << 12); printf("%#X ", (unsigned short) (a << 8) >> 12 << 8); printf("%#X ", (unsigned short) (a << 4) >> 12 << 4); printf("%#X ", (unsigned short) (a << 0) >> 12 << 0); printf("======== "); b = (unsigned short) (a << 12) >> 12 << 12; // D000 b += (unsigned short) (a << 8) >> 12 << 8; // D000 + C00 b += (unsigned short) (a << 4) >> 12 << 4; // DC00 + B0 b += (unsigned short) (a << 0) >> 12 << 0; // DCB0 + A printf("%#X ", b); printf("======== "); } void fun2(unsigned short a) { unsigned short b; printf("======== "); printf("%X ", (a & 0x000F) << 12); printf("%X ", (a & 0x00F0) << 4); printf("%X ", (a & 0x0F00) >> 4); printf("%X ", (a & 0xF000) >> 12); printf("======== "); b = (a & 0x000F) << 12; b += (a & 0x00F0) << 4; b += (a & 0x0F00) >> 4; b += (a & 0xF000) >> 12; printf("%X ", b); printf("======== "); } /** * 四位十六进制反序:ABCD => DCBA */ int main() { // char 0000 0000 // short 0000 0000 0000 0000 unsigned short a = 0XABCD; // scanf("%4X", &a); printf("%X ", a); fun1(a); fun2(a); }