AnsweredAssumed Answered

Shift count converted to unsigned char

Question asked by Daniel Lundin on Apr 23, 2015
Latest reply on May 4, 2015 by Daniel Lundin

I'm running this code:

 

uint16_t bit_n; ...  uint16_t mask = 1u << bit_n%8;

 

No matter which Codewarrior version I'm using, I'm getting this warning/"info": C4003 Shift count converted to unsigned char.

 

What is this nonsense? See the standard for the shift operators C11 6.5.7: "The integer promotions are performed on each of the operands.". No matter if int happens to be 16 or 32 bit on my particular platform, there is no way the shift count should be unsigned char. First of all it is already of a type larger than char. And regardless of that, it must always be integer promoted. Furthermore, the right operand of the shift in this specific case must already be int, as it was already implicitly promoted to that type by the % operator.

 

Questions:

1. Why isn't Codewarrior following the C standard? Is this some non-standard optimization for small MCUs (HCS08 etc)? Or just a bug?

2. Why is Codewarrior warning me about it? As far as I'm aware, I never told Codewarrior to deviate from the standard.

 

I actually don't care about what type the right-hand operand has, as long as the result is correct. But I'm following this coding standard which is stating that the project must be 100% compliant to ISO C and free of compiler warnings.

Outcomes