Connecting Tech Pros Worldwide Forums | Help | Site Map

Unary minus applied to unsigned int

Andrew Ward
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Tõnu Aas
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Unary minus applied to unsigned int


> I tried to compile the following line:[color=blue]
> pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]

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

Tõnu.


Ron Natalie
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Unary minus applied to unsigned int



"Andrew Ward" <anwar@ihug.co.nz> wrote in message news:r1ucb.158330$JA5.3889031@news.xtra.co.nz...
[color=blue]
> But in my c++ book is says that the postfix L forces the integer to be
> signed.[/color]
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.



Jerry Coffin
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Unary minus applied to unsigned int


In article <3f72b7c0$1@news.infonet.ee>, tonu@ids.ee says...

[ ... ]
[color=blue]
> 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
> Its some compiler bug, because -2147483648L is nice signed long constant.[/color]

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.
Mike Wahler
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Unary minus applied to unsigned int



"Tõnu Aas" <tonu@ids.ee> wrote in message news:3f72b7c0$1@news.infonet.ee...[color=blue][color=green]
> > I tried to compile the following line:
> > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]
>
> 2147483648L > MAX LONG, so compiler silently uses unsigned long instead.
> Its some compiler bug, because -2147483648L is nice signed long constant.[/color]

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

-Mike


Jack Klein
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Unary minus applied to unsigned int


On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <anwar@ihug.co.nz>
wrote in comp.lang.c++:
[color=blue]
> Hi All,
> I tried to compile the following line:
> pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]

Replace -2147483648L with (-2147483647 - 1)
[color=blue]
>
> 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[/color]

--
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
Ron Natalie
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Unary minus applied to unsigned int



"Jack Klein" <jackklein@spamcop.net> wrote in message news:dmd7nv8ocdverf4a62d8n06tb6lnn1q7hf@4ax.com...[color=blue]
> On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <anwar@ihug.co.nz>
> wrote in comp.lang.c++:
>[color=green]
> > Hi All,
> > I tried to compile the following line:
> > pair<long, ulong> cr3(make_pair(-2147483648L, 2147483647));[/color]
>
> Replace -2147483648L with (-2147483647 - 1)[/color]

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


Closed Thread