把一个无符号16bit的数像镜面反射一样颠倒一下

下面这个函数是把一个无符号16bit的数像镜面反射一样颠倒一下。

该函数是NXP(飞思卡尔)的 S32DS IDE中提供的SDK中的一个官方代码:

 1 static inline uint16_t REV_BIT_16(uint16_t value)
 2 {
 3     uint8_t i;
 4     uint16_t ret = 0U;
 5 
 6     for (i = 0U; i < 8U; i++)
 7     {
 8         ret |= (uint16_t)((((value >> i) & 1U) << (15U - i)) | (((value << i) & 0x8000U) >> (15U - i)));
 9     }
10 
11     return ret;
12 }

使用S32DS创建普通的C语言的工程。选择CDT Internal Builer(否者编译会错误)。
某数:
‭0b0001001000110100‬
将该数颠倒以后:
‭0b0010110001001000‬

另外还有比较特殊的set和clear:

1     result_set |= (mask); /* bits setting using masking. when the bit in mask is 1, the corresponding bit is to be set */
2     result_clear &= (~(mask)); /* bits clearing using masking. when the bit in mask is 1, the corresponding bit is to be cleared */

 

posted @ 2019-02-14 20:16  panrui  阅读(318)  评论(0编辑  收藏  举报