![]() |
Rule 7.0.4 - exception for binary bitwise operators and constant operands? - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188) +---- Forum: 4.7 Standard conversions (https://forum.misra.org.uk/forumdisplay.php?fid=193) +---- Thread: Rule 7.0.4 - exception for binary bitwise operators and constant operands? (/showthread.php?tid=1717) |
Rule 7.0.4 - exception for binary bitwise operators and constant operands? - cgpzs - 05-12-2024 Hi, My interpretation of Rule 7.0.4 is that it does not allow this type of code: Code: my_function(FLAG_A | FLAG_B | FLAG_C); This is quite frequent when interfacing with third-party as well as POSIX C APIs. Casting each argument to unsigned type would go in detriment of readability. Would it make sense to add an exception for signed constants? Thanks! RE: Rule 7.0.4 - exception for binary bitwise operators and constant operands? - misra cpp - 20-12-2024 We don't wish to encourage the use of signed values in bitwise expressions, even if constant - so are not planning an exception. We are assuming that FLAG_A etc. are C-style enums, so the code also violates 10.2.3 "The numeric value of an unscoped enumeration with no fixed underlying type shall not be used" You need to deviate each such expression, or better define your own bitwise operator for this enum type (which will still need a deviation for 10.2.3 - but only in one place) RE: Rule 7.0.4 - exception for binary bitwise operators and constant operands? - cgpzs - 20-12-2024 Sorry, I should have been more explicit. FLAG_A, etc are C-style positive signed integer #defines, like: #define FLAG_A 0x0001 #define FLAG_B 0x0002 #define FLAG_C 0x0004 Thus we cannot unfortunately define a bitwise operator for them. RE: Rule 7.0.4 - exception for binary bitwise operators and constant operands? - cgpzs - 07-01-2025 @misracpp does my above clarification change your view on the issue? I also note that rule 7.0.5 does have an exception for constant non-negative signed values that are converted to unsigned: > A compile-time constant with signed integral type that has a non-negative value may be converted to an unsigned type through the usual arithmetic conversions. Would it be reasonable to apply the same exception for 7.0.4? RE: Rule 7.0.4 - exception for binary bitwise operators and constant operands? - misra cpp - 10-01-2025 Its an interesting use case, but we suggest you should apply a wrapper around the third-party code, to convert the flags to unsigned constants or enums with unsigned underlying type |