29-10-2015, 03:08 PM
This discussion assumes the following types.
and that signed short has 16 bits.
The essential type of u8a is "unsigned char", whereas the essential type of 0xFF is "signed short" (see Appendix D.6)
Therefore "u8a & 0xFF" will violate both
Rule 10.1 RH operand of & is signed
and
Rule 10.4 both operands do not have the same essential type category
Appendix D.7 explains that if both operands do not have the same signedness the resulting essential type of the operation is the standard C type for that operation.
According to the C standard the usual arithmetic operations are performed on the operands.
u8a is promoted to a C standard type of "signed int" - assuming the above type definitions
0xFF has a C standard type of "signed int"
Therefore both the resultant C standard type and the essential type is "signed int".
The assignment will occur from the essential type of "signed int" to "signed int", which is compliant with 10.3.
An assigning conversion will occur from the essential type of "signed int" to "unsigned char". This violates rule 10.3 because the conversion is to a type of a different essential type category.
Code:
typedef unsigned char uint8_t;
typedef signed int sint32_t;
The essential type of u8a is "unsigned char", whereas the essential type of 0xFF is "signed short" (see Appendix D.6)
Therefore "u8a & 0xFF" will violate both
Rule 10.1 RH operand of & is signed
and
Rule 10.4 both operands do not have the same essential type category
Appendix D.7 explains that if both operands do not have the same signedness the resulting essential type of the operation is the standard C type for that operation.
According to the C standard the usual arithmetic operations are performed on the operands.
u8a is promoted to a C standard type of "signed int" - assuming the above type definitions
0xFF has a C standard type of "signed int"
Therefore both the resultant C standard type and the essential type is "signed int".
Code:
sint32_t s32a = u8a & 0xFF;
Code:
uint8_t u8b = u8a & 0xFF;
Posted by and on behalf of the MISRA C Working Group