Boost logo

Boost :

From: Beman Dawes (bdawes_at_[hidden])
Date: 2006-05-27 16:51:43


"Douglas Gregor" <doug.gregor_at_[hidden]> wrote in message
news:325920BA-D24C-475E-AB8C-85D9429C224B_at_cs.indiana.edu...
>
> On May 26, 2006, at 7:26 AM, Maarten Kronenburg wrote:
>> Every unsigned integer is an integer,
>
> No.
>
> 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:

#include <iostream>
#include <cassert>
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;
   return 0;
}

It does not assert (GCC 3.4.4) and the output is: 4294967295 4294967295 0.

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk