我在C语言中具有以下大字节序:
int32_t num = 0x01234567;
我想将其转换为0x45670123
如何在C语言中使用按位运算符
答案 0 :(得分:1)
一种非常简单的方法是:
num
中读取一个字节。示例:
uint32_t num = 0x01234567;
uint32_t output = 0;
uint32_t firstByte = num & 0xff000000; // firstByte is now 0x01000000
// Where do we want to have 0x01 in the output number?
// 0x45670123
// ^^ here
// Where is 0x01 currently?
// 0x01000000
// ^^ here
// So to go from 0x01000000 to 0x00000100 we need to right shift the byte by 16 (4 positions * 4 bits)
uint32_t adjByte = firstByte >> 16; // adjByte is now 0x0100
// OR with output
output |= adjByte;