MISRA C has the "essential type model" over and above the C standard's rules on arithmetic conversion, integer promotions, etc.
Why does it apply "essential types" smaller than int to integer constants, when the C standard (N1256 6.4.4.1 Integer Constants) dictates that the types of an integer constant are always int, long int, long long int, or their unsigned counterparts?
MISRA has these rules:
10.1 Operands shall not be of an inappropriate essential type
[big table follows, with notes 6 and 7 disallowing shifts with signed operands]6. Shift and bitwise operations should only be performed on operands of essentially unsigned type. The numeric value resulting from their use on essentially signed types may be undefined or implementation-defined.
7. The right hand operand of a shift operator should be of essentially unsigned type to ensure that undefined behavior does not result from a negative shift.
12.2 The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand.
OK, great, so these are not MISRA compliant:
uint16_t x, y, z;
...
x = 13u;
y = 1000u;
z = y >> 10; // non-compliant; shift amount is signed
z = 12345 >> x; // non-compliant; lhs of shift is signed
z = y >> 16u; // non-compliant; shift-right of a uint16_t by 16 is undefined
But then you have this little quirk:
z = 1u << 10u; // non-compliant; essential type of "1u" is unsigned char
// and you can't shift an unsigned char by more than 7 bits
Huh? The type of the integer constant 1u in C is unsigned int which is at least 16 bits (see n1256 5.2.4.2.1), rather than MISRA's idea that the "essential type" of 1u is an unsigned char --- why is 1u << 10u deemed noncompliant?