Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does uint16_t make a difference here?

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.

like image 917
AndreKR Avatar asked May 23 '11 00:05

AndreKR


1 Answers

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".

like image 73
ergosys Avatar answered Oct 06 '22 10:10

ergosys