473,756 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8792
> 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********@new s.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.e e...
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #6

"Jack Klein" <ja*******@spam cop.net> wrote in message news:dm******** *************** *********@4ax.c om...
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
3905
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 aware of. One of them is the unary plus operator, that calls the __pos__ method. It's something that can be highly useful in my own experiments with the use of classes as a tool for generic declarative descriptions of objects -- UI forms,...
10
4659
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 _Fn1 &)' : could not deduce template argument for 'overloaded function type' from 'overloaded
17
1971
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 overflow I am doing the follow: short int x = -4; /* want abs(x) */ unsigned short int abs_val; ....
2
2371
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
5095
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 touched the standards a year ago, though I graduated in embedded SW development... Anyway, to the problem at hand: I've stumbled upon the following construct at work recently, and cannot really make up my mind about the standard's take on the matter.
6
2589
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 example: #include <iostream> using namespace std; class Object { public:
28
5079
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 the ++, the compiler says: "Error: invalid l-value in increment". int i = 10; ~!*&i++;
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.