4

Possible Duplicate:
Integer summing blues, short += short problem

I feel dumb that this is happening, but I have never had this happen before. I am trying to do the following:

foreach (short a in answers)
{
     if (a != myConstants.NOTCOMPLETE_SHORT)
     {
         result = result + a;
     }
     else
     {
         empty = true;
         break;
     }
}

answers is an array of shorts. intellisense is telling me that result + a is an int and I can't assign it to a short.

I have to be missing something very fundamental here but not allowing two shorts to be added together and assigned to a short variable just seems weird.

Community
  • 1
  • 1
peroija
  • 1,982
  • 4
  • 21
  • 37
  • 4
    See Eric Lippert's answer here as to why this happens: http://stackoverflow.com/questions/4343624/integer-summing-blues-short-short-problem – keyboardP Jan 17 '12 at 19:37

4 Answers4

4

Since a short is a 16bit Integer, if you added, say, 32,000 and 32,000 (both valid shorts), you'd get 64,000 which is not a valid short as Int16.MaxValue is 32767.

Thus, the addition operator must return a 32bit Int to prevent the result from possibly overflowing.

UPDATE:

For fun, I just tried this in PowerShell:

PS C:\> ([System.Int32]::MaxValue + [System.Int32]::MaxValue).GetType().Name
Double

PS C:\> (10000000 + 10000000).GetType().Name
Int32

So looks like an Int32 can cast to a Double if needed.

I'm gonna go out on a limb and say bounds checking during addition is more expensive, and thus should only be done if there's a likely chance of an overflow (like two bytes or two shorts) especially when they're possibly using the same amount of memory anyway. I think that's just the way the language was designed.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • 2
    I had the same thought but then I could apply the same logic to ints, that if I added two ints together the value might be larger than int so I would have to assign it to a bigint. – peroija Jan 17 '12 at 19:40
  • definitely think is a correct reason for returning an int since an addition could* result in a value needing more than 16bits...but why does must* apply to addition with shorts but not ints? genuine ly curious – Nadir Muzaffar Jan 17 '12 at 19:41
  • Also I'm mistaken, Int16.MaxValue is actually 32767 :) – Mike Christensen Jan 17 '12 at 19:42
  • @peroija - In my tests, it seems `Int32` can cast to a `Double`. – Mike Christensen Jan 17 '12 at 19:47
0

The arithmetic could wrap around ( to -ve values ) if the result is greater than the largest possible short value. Since it is more likely that this will happen with short, and since int has a larger range, short + short is an int.

manojlds
  • 290,304
  • 63
  • 469
  • 417
0

Either overload operator+= or change the code so that:

short a = 6000;
short b = 6000;
result = (short) result + a //answer 12,000
pmr
  • 58,701
  • 10
  • 113
  • 156
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

As already indicated in the other answers, there are good reasons why two shorts make an int. The addition assignment operator += overload returns a short so your code would become:

foreach (short a in answers)
{
    if (a == myConstants.NOTCOMPLETE_SHORT)
    {
         empty = true;
         break;
    }
    result += a;
}
David Clarke
  • 12,888
  • 9
  • 86
  • 116