From: Beman Dawes (bdawes_at_[hidden])
Date: 2006-05-27 16:51:43
"Douglas Gregor" <doug.gregor_at_[hidden]> wrote in message
> On May 26, 2006, at 7:26 AM, Maarten Kronenburg wrote:
>> Every unsigned integer is an integer,
> A (signed) integer will have a negation operator, such that 0 - x = -
> x, x + -x = 0, etc. An unsigned integer will not have this operator.
That isn't correct, AFAICT. An unsigned does support unary negation.
5.3/9 says "The operand of the unary - operator shall have arithmetic or
enumeration type and the result is the negation of its operand. Integral
promotion is performed on integral or enumeration operands. 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."
Try the following program:
int main( int argc, char * argv )
unsigned u = 1;
assert( (0 - u) == -u );
assert( (u + -u) == 0 );
std::cout << -u << " " << 0-u << " " << u + -u << std::endl;
It does not assert (GCC 3.4.4) and the output is: 4294967295 4294967295 0.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk