0

What happens when I use the unary minus operator with an unsigned zero integer literal, e.g. -0u?

The 2003 C++ standard says in 5.3.1c7:

The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand.

But in the zero case, 2^n - 0 = 2^n, which can't fit in, because the values that can be held are [0,2^n-1].

So, is using the unary minus operator with unsigned zero integer an error? Or does the value that results(2^n) get converted using the modulo because we are trying to fit an out-of-range value in an unsigned?

Mason
  • 501
  • 3
  • 11
  • Does this answer your question? [C: unary minus operator behavior with unsigned operands](https://stackoverflow.com/questions/8026694/c-unary-minus-operator-behavior-with-unsigned-operands) – Achal Nov 08 '19 at 06:04
  • 4
    Please provide line(s) of code to clarify just what you mean. `unsigned zero=0; auto minus = -zero;` ? `auto neged = -0u;` ? – hyde Nov 08 '19 at 06:08
  • 1
    European or African? – Emile Cormier Jan 14 '20 at 22:43

0 Answers0