How to swap nibbles in C?

后端 未结 4 1878
梦谈多话
梦谈多话 2021-01-07 04:41

How to swap the nibble bit positions of a number?

For example: 534, convert it into binary, the rightmost 4 bits has to be interchanged with the leftmost 4 bits and

4条回答
  •  一个人的身影
    2021-01-07 05:01

    If swap is more like a 32 bit endiness conversion the below API must work:

    uint32 decode_32le(uint8 *p)
    {
    return ( p[0] | (p[1] << 8) | (p[2] << 16) |(p[3]<<24));
    }
    

    *here it will swap the consecutive memory location/bytes which is allocated by malloc/calloc only, not an array.

提交回复
热议问题