<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Vince Files &#187; long</title>
	<atom:link href="http://www.thevincefiles.net/tag/long/feed" rel="self" type="application/rss+xml" />
	<link>http://www.thevincefiles.net</link>
	<description>My rants, ramblings, and reflections</description>
	<lastBuildDate>Sun, 28 Jun 2009 22:37:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding 2 integers (32-bit)</title>
		<link>http://www.thevincefiles.net/2008/01/27/adding-2-integers-32-bit</link>
		<comments>http://www.thevincefiles.net/2008/01/27/adding-2-integers-32-bit#comments</comments>
		<pubDate>Mon, 28 Jan 2008 04:45:29 +0000</pubDate>
		<dc:creator>Vince</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[long]]></category>

		<guid isPermaLink="false">http://www.thevincefiles.net/index.php/2008/01/27/adding-2-integers-32-bit/</guid>
		<description><![CDATA[A 32-bit signed integer has valid values ranging from−2,147,483,648 to +2,147,483,647.
So why do I always see sample code of an integer addition method written in C# like this:


private static int add(int a, int b){
    return a + b;
}
So if I were to add 2,000,000,000 and 2,000,000,000 &#8211; an integer datatype would overflow. [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>A 32-bit signed integer has valid values ranging from−2,147,483,648 to +2,147,483,647.</p>
<p>So why do I always see sample code of an integer addition method written in C# like this:</p>
<p><span id="more-11"></span></p>
<pre name="code" class="c-sharp">
private static int add(int a, int b){
    return a + b;
}</pre>
<p>So if I were to add 2,000,000,000 and 2,000,000,000 &#8211; an integer datatype would overflow.  But the solution isn&#8217;t merely to set the return value to a &#8220;long&#8221; (i.e. 64-bit integer):</p>
<pre name="code" class="c-sharp">
private static long add(int a, int b){
    return a + b;
}</pre>
<p>You have to cast each parameter variable to a long before performing the addition:</p>
<pre name="code" class="c-sharp">
private static long add(int a, int b){
    return (long)a + (long)b;
}</pre>
<p>Here are some I found:</p>
<p><a title="Not a good example of C# integer addition" href="http://www.cs.tufts.edu/comp/194NET/notes/csharp.php3" target="_blank">http://www.cs.tufts.edu/comp/194NET/notes/csharp.php3</a></p>
<p><a href="http://www.harding.edu/fmccown/java1_5_csharp_comparison.html" target="_blank"> http://www.harding.edu/fmccown/java1_5_csharp_comparison.html</a></p>
<p>http://www.developernotes.com/post/Use-Ruby-to-Unit-Test-C.aspx</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thevincefiles.net/2008/01/27/adding-2-integers-32-bit/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
