加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-13 20:08:24 所属栏目:Windows 来源:网络整理
导读: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
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,特别是“超大转移金额”部分.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读