473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

2147483647 + 2147483647 = -2?


how come when i add two Int32 vars in a try catch statement with the value
of 2147483647 it results in -2 instead of an exception?
Nov 16 '05 #1
6 2685
Jack,

Can you show the code you are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jack Wasserstein" <Jack Wa*********@discussions.microsoft.com> wrote in
message news:7B**********************************@microsof t.com...

how come when i add two Int32 vars in a try catch statement with the value
of 2147483647 it results in -2 instead of an exception?

Nov 16 '05 #2
Hi,

Overflow

From MSDN:
The Int32 value type represents signed integers with values ranging from
negative 2,147,483,648 through positive 2,147,483,647.

By default the int operations are not checked by overflow, for performance
reasons, you can set flag and the compiler will generate code to check that
it does not happen, it will throw an exception in that case.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jack Wasserstein" <Jack Wa*********@discussions.microsoft.com> wrote in
message news:7B**********************************@microsof t.com...

how come when i add two Int32 vars in a try catch statement with the value
of 2147483647 it results in -2 instead of an exception?

Nov 16 '05 #3
Jack Wasserstein wrote:
how come when i add two Int32 vars in a try catch statement with the value
of 2147483647 it results in -2 instead of an exception?


1) 2147483647 + 2147483647 stored in a signed 32-bit location overflows the
positive range, and results in a wrapped value which is -2 in this case.
Research integer overflow for more & complete details.

2) Integer overflow doesn't cause an exception.
Nov 16 '05 #4
Hi Jack,

You can force an overflow check and throw an exception.
The keyword to use is 'checked'/'unchecked'

int a = 2147483647;
int b = 2147483647;
int i = checked(a + b);
MessageBox.Show(i.ToString());

You can also cause all integer aritmetic to perform this check using the
/checked compiler option.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
in addition, if you want overflowexception, you can either specify the
compiler flag csc /checked+, or use 'checked' keyword in your code, which
will generate the exception.

"Jack Wasserstein" wrote:

how come when i add two Int32 vars in a try catch statement with the value
of 2147483647 it results in -2 instead of an exception?

Nov 16 '05 #6
Julie wrote:
Jack Wasserstein wrote:
how come when i add two Int32 vars in a try catch statement with the
value of 2147483647 it results in -2 instead of an exception?

1) 2147483647 + 2147483647 stored in a signed 32-bit location overflows
the positive range, and results in a wrapped value which is -2 in this
case. Research integer overflow for more & complete details.

2) Integer overflow doesn't cause an exception.


I was not aware of the checked option as others have pointed out. I amend my
original response to:

2) Integer overflow doesn't cause an exception by default.
Nov 16 '05 #7

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

Similar topics

0
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
7
by: Faree | last post by:
Hi all, I am doing portal for storing info of visitors.it is working well too.but my prob is with mysql..when i entered infoa bt thier phone numbers ,fax or mobile numbers,soemtimes it is...
2
by: Phil Endecott | last post by:
Dear Experts, I have a couple of questions about the efficiency of queries involving views. Say I have a large table T, and a view V that just adds some extra columns to T, using for example...
14
by: ritesh.noronha | last post by:
Hi, I am a beginer programmer, i would like to know the exact difference between long and int. Why do we have two names for the same size of variable. Does the size of long and int change on...
1
by: traceable1 | last post by:
I have 2 SQL databases which are the same and are giving me different query plans. select s.* from hlresults h inner join specimens s on s.specimen_tk = h.specimen_tk where s.site_tk = 9 and...
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
13
by: Muffinthief | last post by:
Just a quick question, how do you go about storing an integer over 2147483647 (2^31-1)?
166
by: Nimmi Srivastav | last post by:
Apologies if my cross posting has offended anyone.... For a pure hobbyist C/C++ programmer, who wants to develop applications to run on Windows, what would be a better choice to install: Visual...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.