Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unary minus applied to unsigned int

Reply
Thread Tools

Unary minus applied to unsigned int

 
 
Andrew Ward
Guest
Posts: n/a
 
      09-25-2003
Hi All,
I tried to compile the following line:
pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));

but get this error:
unary minus applied to unsigned type, result still unsigned.

But in my c++ book is says that the postfix L forces the integer to be
signed.

Could anyone please explain this behaviour, and if possible point me to some
documentation that explains how intergral types are handled in depth?

Andy


 
Reply With Quote
 
 
 
 
Tõnu Aas
Guest
Posts: n/a
 
      09-25-2003
> I tried to compile the following line:
> pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));


2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
Its some compiler bug, because -2147483648L is nice signed long constant.

Tõnu.


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      09-25-2003

"Andrew Ward" <(E-Mail Removed)> wrote in message news:r1ucb.158330$(E-Mail Removed)...

> But in my c++ book is says that the postfix L forces the integer to be
> signed.

The number is too big for (signed) long so it becomes unsigned long.
There are no negative literals, just positive ones with a unary minus
applied to it. There is no way to represent that number with a literal
I expect on your environment.

However, you can use numeric_limits<long>::min() to get the value.
Probably a better idea than hardcoding those number in anyhow.



 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-25-2003
In article <3f72b7c0$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...

[ ... ]

> 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
> Its some compiler bug, because -2147483648L is nice signed long constant.


Unfortunately this isn't correct. The problem is that this is NOT
parsed as simply a negative constant. It's parsed as a unary minus
operator, followed by a positive constant. Unfortunately, on a typical
32-bit system, 2147483648L is too large to be a signed long constant, so
it's treated as an unsigned long constant. The unary minus is then
applied to that unsigned long, and about the only (sort of) good result
is a warning message telling you that you haven't gotten what you
probably wanted.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-25-2003

"Tõnu Aas" <(E-Mail Removed)> wrote in message news:3f72b7c0$(E-Mail Removed)...
> > I tried to compile the following line:
> > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));

>
> 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
> Its some compiler bug, because -2147483648L is nice signed long constant.


That value is not required to be in the range of
signed long. A value one greater is, however.

-Mike


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-26-2003
On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <(E-Mail Removed)>
wrote in comp.lang.c++:

> Hi All,
> I tried to compile the following line:
> pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));


Replace -2147483648L with (-2147483647 - 1)

>
> but get this error:
> unary minus applied to unsigned type, result still unsigned.
>
> But in my c++ book is says that the postfix L forces the integer to be
> signed.
>
> Could anyone please explain this behaviour, and if possible point me to some
> documentation that explains how intergral types are handled in depth?
>
> Andy


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-26-2003

"Jack Klein" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <(E-Mail Removed)>
> wrote in comp.lang.c++:
>
> > Hi All,
> > I tried to compile the following line:
> > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));

>
> Replace -2147483648L with (-2147483647 - 1)


If he really wants the minimum long and maximum ulong variables
he really should use numeric_limits min and max.


 
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
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Compiling error key(int,int) in ATM2.ATMPanel cannot be applied to (int) powerhouse04 Java 1 12-16-2007 06:03 PM
Overloading unary minus for use in function calls Matthew Cook C++ 6 12-20-2006 10:04 PM
Semantics of unary minus Marc C Programming 13 02-13-2006 04:52 PM
Lazy fun: Make unary minus silence stderr for backticks Sam Stephenson Ruby 9 11-07-2005 03:36 PM



Advertisments