473,939 Members | 16,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Usual Arithmetic Conversions-arithmetic expressions

After reading the Standard section 6.3 multiple times and consulting
K&R I can't seem to figure out what would happen in the following case
assuming longs are 32 bits(have typedefs for this):

unsigned long int result;
signed long int x, y;

x = 0x7FFF8000;
y = -0x8000;

result = x - y;

I am trying to figure out what happens in this case. From my
understanding both operands will be converted to their common real type
which in this case will be 'signed long int'(no conversion). After the
subtraction is performed then the result will be converted to an
'unsigned long int'. Since this subtraction results in a positive
number that can't be represented in a signed long(+0x8000000 0), what
happens in this case? Can someone walk me through this step by step?
That would really help me understand this.

Also, anyone have a good suggestion for a book that goes over this type
of thing? My reading of the standard and K&R didn't help me. Maybe some
kind of annotated standard but obviously the Schildt book is worthless.

Thanks.

Nov 14 '05 #1
5 1660
Correct me if I'm wrong, but I'm pretty shure the result here is
undefined by the standard and should not happen. I guess you will have
to test it on the compilers you want to use it on.

--
bjrnove

Nov 14 '05 #2

bjrnove wrote:
Correct me if I'm wrong, but I'm pretty shure the result here is
undefined by the standard and should not happen. I guess you will have to test it on the compilers you want to use it on.

--
bjrnove


Yeah I guess my thread subject should have been about overflow since my
question is about overflow as opposed to UAC it seems. I found some old
threads on overflow but I'm not sure where in the standard people are
getting their information about this.

Nov 14 '05 #3
A unsigned type will never overflow. So the code below will be handled
the same way on every system:

unsigned int ui = 0;

while(ui >= 0) /* Always true */
printf("%i\n", ui);

It will print all the numbers you can fit in an int and then start on 0
again. If you do the same thing again with a unsigned int you will get
various results, and I'm pretty shure the standard doesn't say anything
about what should happend.

Here is some words about the issue:
http://publications.gbdirect.co.uk/c...ral_types.html

--
bjrnove

Nov 14 '05 #4
joshc wrote:
After reading the Standard section 6.3 multiple times and
consulting K&R I can't seem to figure out what would happen
in the following case assuming longs are 32 bits:

unsigned long int result;
signed long int x, y;

x = 0x7FFF8000;
y = -0x8000;

result = x - y;

I am trying to figure out what happens in this case. From
my understanding both operands will be converted to their
common real type which in this case will be 'signed long
int'(no conversion). After the subtraction is performed
then the result will be converted to an 'unsigned long int'.
Since this subtraction results in a positive number that
can't be represented in a signed long(+0x8000000 0), what
happens in this case?
Undefined behaviour (signed integer overflow). The UB
occurs before the conversion to unsigned long (so there
might never be a conversion to unsigned long and never
be an assignment to 'result').
Can someone walk me through this step by step?


You just walked yourself through it quite well.

Nov 14 '05 #5
"Old Wolf" <ol*****@inspir e.net.nz> writes:
joshc wrote:
After reading the Standard section 6.3 multiple times and
consulting K&R I can't seem to figure out what would happen
in the following case assuming longs are 32 bits:

unsigned long int result;
signed long int x, y;

x = 0x7FFF8000;
y = -0x8000;

result = x - y;

I am trying to figure out what happens in this case. From
my understanding both operands will be converted to their
common real type which in this case will be 'signed long
int'(no conversion). After the subtraction is performed
then the result will be converted to an 'unsigned long int'.
Since this subtraction results in a positive number that
can't be represented in a signed long(+0x8000000 0), what
happens in this case?


Undefined behaviour (signed integer overflow). The UB
occurs before the conversion to unsigned long (so there
might never be a conversion to unsigned long and never
be an assignment to 'result').


It's also worth mentioning that, on many systems, the undefined
behavior will manifest itself as a wraparound. On such systems, the
result of the subtraction will be -0x80000000 (LONG_MIN), which when
converted to unsigned long will yield 0x80000000. (Once you get the
-0x80000000, the conversion is well defined; it's only the subtraction
that triggers undefined behavior.) This assumes a 2's-complement
representation.

Undefined behavior is usually to be avoided, but it can be useful to
know how it's likely to behave in real life (while keeping firmly in
mind that undefined really does mean undefined, and the standard makes
no guarantees).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2628
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are implicitly converted within each other and bools can be implicitly converted to ints? However, I'm unable to find any other implicit conversions and the order of the implicit conversions (something like int->float->long). Any help would be greatly...
10
2243
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to me, since any pointer can be cast to void* and void * can be cast to any other pointer so this implies that any pointer can be cast to any other pointer. Is that so?
4
2006
by: O. Zimmermann | last post by:
Hi all, A variable with an enumerated type can be set with a value from any other generic "enum" type in the program, or with an integer value, without notice to the user, neither at compilation nor at execution. How can I make this IMPOSSIBLE (so that ONLY a value from the expected enumerated type can be fed to the variable, with no implicit conversion allowed ?).
31
3660
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
2
2326
by: Brian Henry | last post by:
is there info about data type conversions between access to ado.net? I have tried a lot of the ones that say oledb data types should work for and get a lot of "data type mismatch" errors using them.. is there a sheet somewhere or a refrence for them? all i really need to know is Access ADO.NET Numeric Integer/numeric? Memo VarWChar? Text VarWChar? Date/Time ?...
36
3662
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work in a C# program but not VB? -- Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/ "Programming is an art form that fights back"
5
1634
by: Daz | last post by:
Hi everyone! I am very new to casting and converting, so much in fact then when I think I have it figured out, something proves me wrong. I was wondering if there is any logic in 'what types can be converted/casted into what other types'. How do I know what casts I can use on a char array for example. Is it something you just have to figure out, need to learn, or is there somewhere that maybe most possible conversions are documented. I...
2
4494
by: adsci | last post by:
Hello! Im posting this to c.l.c++ AND win32 because i dont know if this is a MS Compiler Issue or not. here we go: <code> class MyCString
3
1342
by: Wayne | last post by:
Are user-defined conversions chosen at compile time, or are there ways to make sure that they are chosen at run-time, based on the actual type of the object? Here is a simplified example of what I'm trying to accomplish: I have a method that has a parameter of type object. If the parameter is a boxed uint, it should unbox it. So, the method looks like this: void f(object o)
0
1001
by: Greg Taylor | last post by:
Greetings, I'm not sure if this is the right place to probe for interest, but I figured I'd give it a shot. Recently, a project I undertook at work required us to convert between the various CIE color spaces (XYZ, Lab, LCH, Luv, etc.). I looked long and hard but didn't find any Python library that did this, I was able to find JavaScript and C-based implementations, but that's about it. For a good example of the kinds of conversions I...
0
9963
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,...
1
11292
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
9858
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...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7387
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
6076
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...
0
6297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4447
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.