472,954 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 8742
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't...
10
by: SpOiLeR | last post by:
I have function bool IsGood (const std::string& sr); I want to use that function in std::not1 STL functor. I tried this: not1(IsGood) /* error : 'std::unary_negate<_Fn1> std::not1(const...
17
by: joshc | last post by:
I searched through the newsgroup for this and found the answer but wanted to make sure because of something that came up. I want the absolute value of a 'short int' so to avoid the dangers of...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
13
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even...
6
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.