MISRA Discussion Forums

Full Version: Underlying type balancing for integral operands
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
From "Underlying type balancing" on page 57:

Quote:Otherwise, if both operands have integral type, the underlying type of the expression can be found using the following:
– If the types of the operands are the same size, and either is unsigned, the result is unsigned.
– Otherwise, the type of the result is that of the larger type.

1. Does "the types of the operands" refer to underlying type or actual type?

2. Does "and either is unsigned" mean "and either is an unsigned integral type" or "and either is the type unsigned [int]"?

3. What is the intended meaning of "the result is unsigned" in the first bullet? A literal reading might suggest that the result is simply unsigned, i.e. the type unsigned int. I do not think this is the intended behavior. I assume it was meant to have the effect as if it said "the result is the [underlying?] type of the unsigned operand(s)".

4. Does "that of the larger type" refer to underlying type or actual type?
To answer your questions:

1) underlying type

2) "and either is an unsigned integral type"

3) "the result is the actual type of the unsigned operand(s)"

4) underlying type
What is the intended interaction between 1 and 3, particularly the transition from underlying type to actual type for the "unsigned operand"? This seems to lead to strange outcomes for code like a_u16 + b_u16 + c_u16 where there are expressions whose underlying type is unsigned while the actual type is signed. The only way I see to interpret 1 and 3 together as written would be as "if the essential type of either operand is unsigned, the result is the actual type of the essentially unsigned operand". Presumably either "either is unsigned" should refer to actual type, or "the result is unsigned" should refer to the essential type of the unsigned operand. The latter seems to produce the most consistent results when applied to the rules in 6.5.
The wording of our previous post could have been clearer -
for 3, its the underlying type of the result that is unsigned