18-02-2016, 08:49 AM
This answer assumes that C types has the following sizesFirst it is important to understand what is happening in terms of the C standard type.
The integer literal "3" is of type "signed int", which is then cast to "unsigned short". Before the "~" operator is called the "usual unary conversions" are applied to the operand and hence the "(unsigned char)3" is promoted back to have type "signed int".
Every bit in the 32-bit number is then inverted, and the type remains the same as the promoted type; in other words it remains as "signed int". On most machines that bit pattern will be equivalent to -4, but in general the value is implementation dependent.
In contrast the essential type of the operator "~" is essentially unsigned short. The operand is an integer constant expression, which means that essential type can be determined by looking at Appendix D.7 and paragraph 1.1 of the section on ~.
Similarly "(unsigned char)3 - (unsigned char)4" will have a resultant C standard type of "signed int", but an essential type of "unsigned int". This is because the equivalent unsigned value will be be 4294967292, which gives a UTLR of "unsigned int". See paragraph 2.1 in Appendix D7 of the section on.
Code:
unsigned char: 8 bits
unsigned short: 16 bits
unsigned int : 32 bits
The integer literal "3" is of type "signed int", which is then cast to "unsigned short". Before the "~" operator is called the "usual unary conversions" are applied to the operand and hence the "(unsigned char)3" is promoted back to have type "signed int".
Every bit in the 32-bit number is then inverted, and the type remains the same as the promoted type; in other words it remains as "signed int". On most machines that bit pattern will be equivalent to -4, but in general the value is implementation dependent.
In contrast the essential type of the operator "~" is essentially unsigned short. The operand is an integer constant expression, which means that essential type can be determined by looking at Appendix D.7 and paragraph 1.1 of the section on ~.
Quote:1. If the operand is essentially unsigned then:The result of the "~" operator has a well-defined bit pattern which corresponds to an unsigned value of 4294967292, and therefore has a UTLR of "unsigned int". The use of the result of the "~" operator may then violate other MISRA rules. For example assignment to a "signed int" object will violate rule 10.3. This protects the user from relying on a value that is implementation dependent.
1.1 If the operand is an integer constant expression then the essential type of the result is the UTLR of the result.
Similarly "(unsigned char)3 - (unsigned char)4" will have a resultant C standard type of "signed int", but an essential type of "unsigned int". This is because the equivalent unsigned value will be be 4294967292, which gives a UTLR of "unsigned int". See paragraph 2.1 in Appendix D7 of the section on.
Quote:2. If the operands are both essentially unsigned then:Note: this expression will violate rule 12.4.
2.1 If the expression is an integer constant expression then the essential type of the result is the UTLR of the result.
Posted by and on behalf of the MISRA C Working Group