Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Semantics of unary minus

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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?
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unary minus strangeness sreservoir Perl Misc 7 03-31-2010 11:01 PM
What is happening with this Unary minus? Todd Burch Ruby 3 08-27-2007 09:22 PM
Overloading unary minus for use in function calls Matthew Cook C++ 6 12-20-2006 10:04 PM
Lazy fun: Make unary minus silence stderr for backticks Sam Stephenson Ruby 9 11-07-2005 03:36 PM
Unary minus applied to unsigned int Andrew Ward C++ 6 09-26-2003 03:19 PM



Advertisments