字节序 endian
不同的CPU有不同的字节序类型,这些字节序是指整数在内存中保存的顺序
最常见的有两种:
- Little-endian:将低序字节存储在起始地址(低位编址)
- Big-endian:将高序字节存储在起始地址(高位编址)
字节序的转换:
/*****************************************************************************/
/**
*
* Perform a 16-bit endian converion.
*
* @param Data contains the value to be converted.
*
* @return converted value.
*
* @note None.
*
******************************************************************************/
u16 Xil_EndianSwap16(u16 Data)
{
return (u16) (((Data & 0xFF00U) >> 8U) | ((Data & 0x00FFU) << 8U));
}
/*****************************************************************************/
/**
*
* Perform a 32-bit endian converion.
*
* @param Data contains the value to be converted.
*
* @return converted value.
*
* @note None.
*
******************************************************************************/
u32 Xil_EndianSwap32(u32 Data)
{
u16 LoWord;
u16 HiWord;
/* get each of the half words from the 32 bit word */
LoWord = (u16) (Data & 0x0000FFFFU);
HiWord = (u16) ((Data & 0xFFFF0000U) >> 16U);
/* byte swap each of the 16 bit half words */
LoWord = (((LoWord & 0xFF00U) >> 8U) | ((LoWord & 0x00FFU) << 8U));
HiWord = (((HiWord & 0xFF00U) >> 8U) | ((HiWord & 0x00FFU) << 8U));
/* swap the half words before returning the value */
return ((((u32)LoWord) << (u32)16U) | (u32)HiWord);
}