Unary minus applied to unsigned int

A

Andrew Ward

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

Tõnu Aas

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.
 
R

Ron Natalie

Andrew Ward said:
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.
 
J

Jerry Coffin

[email protected] says... said:
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.
 
M

Mike Wahler

Tõnu Aas said:
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
 
J

Jack Klein

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
 
R

Ron Natalie

Jack Klein said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top