volatile uint16_t r;
unsigned char poly = 0x07;
unsigned char c = 0;
r = (c << 8) ^ poly;
When the code is compiled with gcc on Linux, r
is 7
.
When the same code is compiled by Microchip C18, r
is 0
.
Why?
If I change it to:
volatile uint16_t r;
uint16_t poly = 0x07;
uint16_t c = 0;
r = (c << 8) ^ poly;
r
becomes 7
in C18, too.
There is a section about integer promotion in the C18 manual, but I don't think it has something to do with my question. Anyway, here it is:
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.
Since c << 8
is undefined in this compiler, the result of xor can't be predicted. The result could be anything the compiler chooses.
See What Every C Programmer Should Know About Undefined Behavior for an introduction to undefined behavior, especially the section "Oversized Shift Amounts".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With