...
This compliant solution eliminates the possibility of shifting by more bits than exist in the left hand operand. The PRECISION()
macro is defined in INT35-C. Use correct integer precisions:
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; } /* ... */ } |
...