Velocity Reviews > Semantics of unary minus

# Semantics of unary minus

pete
Guest
Posts: n/a

 02-13-2006
Antonio Contreras wrote:
>
> pete wrote:
> > Antonio Contreras wrote:
> > >
> > > Eric Sosman wrote:
> > >
> > > <snip: question about unary minus overflowing for INT_MIN>
> > >
> > > > Yes, two's complement is asymmetric about zero: there
> > > > is a negative number whose absolute value is not representable.
> > > > (The Standard actually permits an implementation to dodge this
> > > > asymmetry by defining "all ones" to be a trap representation;
> > > > I've never heard of an implementation that does so.)
> > >
> > > ITYM a 1 followed by all 0s. All ones is -1 in two's complement.

> >
> > He's talking about negative zero.
> > A one followed by all zeros, is negative zero
> > in sign and magnitude representation.
> > All ones is negative zero in one's complement representation.
> > There's different ways for an implementation
> > to deal with negative zero, trapping is one of them.

>
> But he was talking about two's complement. And AFAIK the standard
> allows a two's complement representation with trap representations. In
> this case the obvious pattern
> for a trap representation is a 1 followed
> by all 0s, which implies that INT_MIN = - INT_MAX

Yes, I had missed the point.

--
pete

Jordan Abel
Guest
Posts: n/a

 02-13-2006
On 2006-02-12, pete <(E-Mail Removed)> wrote:
> Marc wrote:
>
>> int a;

>
>> -a;

>
>> "if-it-fits-otherwise-overflow"?

>
> Yes.
> "overflow" as in "undefined behavior".
>
> It's a point of trivia which also comes up when writing itoa,
> that there is no integer type which is guaranteed
> to be able to represent the magnitude of INT_MIN.

Not unsigned int?

Eric Sosman
Guest
Posts: n/a

 02-13-2006
Antonio Contreras wrote:
> Eric Sosman wrote:
>
> <snip: question about unary minus overflowing for INT_MIN>
>
>> Yes, two's complement is asymmetric about zero: there
>>is a negative number whose absolute value is not representable.
>>(The Standard actually permits an implementation to dodge this
>>asymmetry by defining "all ones" to be a trap representation;
>>I've never heard of an implementation that does so.)

>
>
> ITYM a 1 followed by all 0s. All ones is -1 in two's complement.

Right you are. Sorry for the blunder.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

pete
Guest
Posts: n/a

 02-13-2006
Jordan Abel wrote:
>
> On 2006-02-12, pete <(E-Mail Removed)> wrote:
> > Marc wrote:
> >
> >> int a;

> >
> >> -a;

> >
> >> "if-it-fits-otherwise-overflow"?

> >
> > Yes.
> > "overflow" as in "undefined behavior".
> >
> > It's a point of trivia which also comes up when writing itoa,
> > that there is no integer type which is guaranteed
> > to be able to represent the magnitude of INT_MIN.

>
> Not unsigned int?

No.
The requirement is for the unsigned type to have
at least as many value bits as the signed type.
You can have:
CHAR_BIT == 16
sizeof(int) == 2
UINT_MAX == 65535
INT_MIN == -65536

--
pete