Title
Computing the negative of 0U
Status
nad
Section
7.6.2.2 [expr.unary.op]
Submitter
comp.std.c++

Created on 2006-12-21.00:00:00 last changed 214 months ago

Messages

Date: 2007-04-15.00:00:00

Rationale (April, 2007):

As noted in the issue description, a “truncating conversion” is needed. This conversion is supplied without need of an explicit mention, however, by the nature of unsigned arithmetic given in 6.8.2 [basic.fundamental] paragraph 4:

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.
Date: 2022-11-20.07:54:16

In 7.6.2.2 [expr.unary.op], part of paragraph 7 describes how to compute the negative of an unsigned quantity:

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

According to this method, -0U will get the value 2n - 0 = 2n, where n is the number of bits in an unsigned int. However, 2n is obviously out of the range of values representable by an unsigned int and thus not the actual value of -0U. To get the result, a truncating conversion must be applied.

History
Date User Action Args
2007-05-06 00:00:00adminsetmessages: + msg1510
2007-05-06 00:00:00adminsetstatus: open -> nad
2006-12-21 00:00:00admincreate