471,595 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Unary minus applied to unsigned int

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
Jul 19 '05 #1
6 8651
> 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.
Jul 19 '05 #2

"Andrew Ward" <an***@ihug.co.nz> wrote in message news:r1**********************@news.xtra.co.nz...
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.

Jul 19 '05 #3
In article <3f********@news.infonet.ee>, to**@ids.ee 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.
Jul 19 '05 #4

"Tõnu Aas" <to**@ids.ee> wrote in message news:3f********@news.infonet.ee...
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
Jul 19 '05 #5
On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <an***@ihug.co.nz>
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
Jul 19 '05 #6

"Jack Klein" <ja*******@spamcop.net> wrote in message news:dm********************************@4ax.com...
On Thu, 25 Sep 2003 16:19:33 +1200, "Andrew Ward" <an***@ihug.co.nz>
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.
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Carlos Ribeiro | last post: by
17 posts views Thread by joshc | last post: by
2 posts views Thread by Javier Estrada | last post: by
13 posts views Thread by Marc | last post: by
6 posts views Thread by Matthew Cook | last post: by
28 posts views Thread by dspfun | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.