Hacker News new | ask | show | jobs
by kazinator 2066 days 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
1 comments

If 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.
Then 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.