为什么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,特别是“超大转移金额”部分.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...