题目:输入16位整数x,如0x1234,将其转为Big Endian格式再输出,此例为输出 0x3412
来源:某500强企业面试题目
思路:将x左移8位得到a,将x右移8位得到b,a+b即为所得
//---------------------------------------- // 写一个宏,将16位的整数转为Big Endian //---------------------------------------- #define ConvertToBigEndian(x) (((x)<<8) & 0xFF00) +(((x)>>8) & 0x00FF) void TestConvertToBigEndian(void) { ; ; // 下面输出为:ConvertToBigEndian(0x1234) = 0x3412 a = 0x1234; b = ConvertToBigEndian(a); printf("ConvertToBigEndian(0x%04X) = 0x%04X\n", a, b); // 下面输出为:ConvertToBigEndian(0x12) = 0x1200 a = 0x12; b = ConvertToBigEndian(a); printf("ConvertToBigEndian(0x%04X) = 0x%04X\n", a, b); }