Integer Summing Blues, Short += Short Problem

Why is my addition of 2 shorts causing a casting compile error due to ints?

It is because short + short = int.

Eric Lippert explains it here.

He says:

Why is short plus short result in int?

Well, suppose short plus short was short and see what happens:

short[] prices = { 10000, 15000, 11000 }; short average = (prices[0] +
prices[1] + prices[2]) / 3; And the average is, of course, -9845 if
this calculation is done in shorts. The sum is larger than the largest
possible short, so it wraps around to negative, and then you divide
the negative number.

In a world where integer arithmetic wraps around it is much more
sensible to do all the calculations in int, a type which is likely to
have enough range for typical calculations to not overflow.


Operation with short int C#

that shows the compilation error because because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default. So you would need to do type casting.

It is possible though to use plus operand with other data types like long int double, where the destination variable has the same storage size or a larger storage size but as short has smaller size, so you can't use it directly.

short + short != short?

Int is the smallest signed type for which the + operator is defined, so trying to use + on a short results in that kind of error.

C# does not let me sum two shorts to a short


And it does not compile, saynig cannot convert 'int' to 'short'. I am maybe really tired today but I cannot see the issue!

It's just the way the language is defined. The + operator on integer types is defined for:

static uint op +(uint x, uint y)
static int op +(int x, int y)
static ulong op +(ulong x, ulong y)
static long op +(long x, long y)

Operands are promoted as required.

Now as for the reasons why it's defined that way - I don't know, to be honest. I don't buy the argument of "because it could overflow" - that would suggest that byte + byte should be defined to return short, and that int + int should return long, neither of which is true.

I've heard somewhere that it could be performance related, but I wouldn't like to say for sure. (Perhaps processors typically only provide integer operations on 32 and 64 bit integers?)

Either way, it doesn't really matter why it's the case - it's just the rules of the language.

Note that the compound assignment operators have an implicit conversion back to the relevant type, so you can write:

short x = 10;
short y = 20;
x += y;

two shorts added together don't equal a short?

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.

Compile time error on addition of two short variable [C#]

By specification short + short -> int. Do short z = (short)(x + y);

Best answer is given by Eric Lippert here: Integer summing blues, short += short problem

short not being printed correctly c++

I'm guessing you are printing the numbers with printf("%X", value).

In that case you are most likely irritated by sign-extension. The short value 0xFFFF which you have specified equals the decimal value -1. If this is extended/casted to a 32bit integer value you will get the same -1 value, but it's binary representation is 0xFFFFFFFF. This is what you see that gets printed. If you print with printf("%hX", value) you should see the correct value, if you don't have another cast from short to int in between.

Cast error in C# for datatype short

Modify the following,

short key = i + (short)1;

to

short key = (short) (i + (short)1);

The reason being, any additions of short + short might overflow short range. and hence this requires a explicit casting.



Related Topics



Leave a reply



Submit