2 ms·
> warning C4146: unary minus operator applied to unsigned type, result still unsigned #ifdef _MSVC x = ~x + 1; // "Manual" two's complement to avoid warn
by kazinator 6y ago
> warning C4146: unary minus operator applied to unsigned type, result still unsigned
#ifdef _MSVC
x = ~x + 1; // "Manual" two's complement to avoid warning.
#else
x = -x; // Regular two's complement any good C coder knows
#endif
- Arnavion 6y agoIf you're going to go to the effort of writing the complement version instead of pragma-suppressing the warning on that line, just remove the ifdef and use that with all compilers.
- kazinator 6y agoThen I would look like someone who doesn't know that unary minus on an unsigned int obtains the two's complement. That aspect can be taken care of by a comment. Why I might go for the #ifdef is to avoid changing working code, except for that one compiler that is complaining.