为什么uint16_t在这里有所作为?

volatile uint16_t r;
unsigned char poly = 0x07;
unsigned char c = 0;

r = (c << 8) ^ poly;

当在Linux上使用gcc编译代码时,r为7.
当Microchip C18编译相同的代码时,r为0.
为什么?

如果我更改为:

volatile uint16_t r;
uint16_t poly = 0x07;
uint16_t c = 0;

r = (c << 8) ^ poly;

在C18中r也变为7.

在C18手册中有关于整数推广的部分,但我不认为它与我的问题有关.无论如何,这里是:

ISO mandates that all arithmetic be performed at int precision or greater.
By default,MPLAB C18 will perform
arithmetic at the size of the largest
operand,even if both operands are
smaller than an int. The ISO mandated
behavior can be instated via the -Oi
command-line option.

由于c < 8在这个编译器中是未定义的,xor的结果是无法预测的.结果可能是编译器选择的任何东西. 有关未定义行为的介绍,请参见 What Every C Programmer Should Know About Undefined Behavior,特别是“超大转移金额”部分.

相关文章

文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作...
文章浏览阅读1k次。解决 Windows make command not found 和...
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计...
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是...
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,...
文章浏览阅读1.4w次,点赞5次,收藏22次。如果使用iterator的...