用 gcc 就非常简单,gcc 有内置函数对 16、32、64 位的整型数据进行字节逆序。
— Built-in Function: uint16_t __builtin_bswap16 (uint16_t x)
Returns x with the order of the bytes reversed; for example, 0xaabb becomes 0xbbaa. Byte here always means exactly 8 bits.
— Built-in Function: uint32_t __builtin_bswap32 (uint32_t x)
Similar to __builtin_bswap16, except the argument and return types are 32 bit.
— Built-in Function: uint64_t __builtin_bswap64 (uint64_t x)
Similar to __builtin_bswap32, except the argument and return types are 64 bit.
gcc 的内置函数会利用目标 CPU 支持字节逆序的指令,比如 cortex-m (thumb/thumb2) 就有 rev、rev16、revsh、revh 指令支持逆序操作。 |