![]() |
is a cast compliant with Rule 12.4 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21) +---- Forum: 8.12 Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=167) +---- Thread: is a cast compliant with Rule 12.4 (/showthread.php?tid=1707) |
is a cast compliant with Rule 12.4 - sowisojh - 30-09-2024 Is the cast in the following code compliant with MISRA2023 Rule 12.4, assuming a machine having int as 32 bit? Code: #include <stdint.h> According to Appendix D.7.5 1.1 the result of ~ is the UTRL of the result. Assuming an 32 bit integer machine this would be 4294967263u. The result of the cast to uint16_t would be perform a "repeatedly [...] subtracting one more than the maximum value that can be represented in the new type until the value is in the new type" which leads to 65503. Is this type conversion treated as "unsigned integer wrap-around" in the sense of Rule 12.4 ? RE: is a cast compliant with Rule 12.4 - misra-c - 10-07-2025 The conversion is defined in terms of mathematical addition or subtraction operations on the value of the expression, but these do not imply that C addition or subtraction operations actually occur, as clarified by footnote 60 (C11) and footnote 51 (C24), whereas this Rule is understood only to apply to operations resulting from the C expression language. In this example, the ~ operator results in extension, which is not a wraparound. Therefore this Rule does not apply. |