...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <limits.h> #include <stddef.h> #include <inttypes.h> extern size_t popcount(uintmax_t); #define PRECISION(x) popcount(x) void func(unsigned int ui_a, unsigned int ui_b) { unsigned int uresult = 0; if (ui_b >= PRECISION(UINT_MAX)) { /* Handle error condition */ } else { uresult = ui_a << ui_b; } /* ... */ } |
The
macro is defined in macro provides the correct precision for any integer type (see INT35-C. Use correct integer precisions). Modulo behavior resulting from left-shifting an unsigned integer type is permitted by exception INT30-EX3 to INT30-C. Ensure that unsigned integer operations do not wrap.PRECISION()
...