在看图形处理源码的时候有一段宏看不懂,就写了一个demo自己看了一下。
还是不大熟悉C语言的位操作。
#include
#include
#ifndef AV_RB16
# define AV_RB16(x) \
((((const uint8_t*)(x))[0] << 8) | \
((const uint8_t*)(x))[1])
#endif
void print_bits(uint16_t *ele) {
printf("%p \n", ele);
printf("%8u \n",((const uint8_t*)ele)[0]);
printf("%8u \n",((const uint8_t*)ele)[1]);
printf("AV_RB16: %d \n", AV_RB16(ele));
}
int main() {
uint16_t e = 255;
print_bits(&e);
}
这个宏挺有意思
define AV_RB16(x) \
((((const uint8_t*)(x))[0] << 8) | \
((const uint8_t*)(x))[1])
endif
注意 括号的位置很重要
((const uint8_t*)(x)) 这相当于把x所在的内存当做一段uint8类型的指针,索引x的第0位,因为是小端机,低位存低字节,所以[0]位存的是低字节,[1]位存的是高字节。
这样想的话,这个宏其实很简单了,就是把16位的无符号数,按照8位一个单位高低位互换
事实也证明如此,程序输出如下
图片.png