471,354 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

(Me || Compiler) == Stupid

private void Test(short i, short j)
{
short k = i + j;
}

What the hell is wrong with this code?
"Can not implicitly convert type int to short" is the compile error.
ASP.NET 1.1 C#

Thanks

Feb 6 '06 #1
10 1379
It isn't very intuitive, granted, but you just need to cast:

short k = (short)(i + j);

Discussed more here (but doesn't really state "why"):
http://msdn2.microsoft.com/en-us/library/ybs77ex4.aspx

Marc
Feb 6 '06 #2
'Short + short' can be bigger than a 'short', and you can not implicitly
convert it to a short value due to the size. You have to do it explicitly:

private void Test(short i, short j)
{
short k = (short) (i + j);
}

"Steven Nagy" wrote:
private void Test(short i, short j)
{
short k = i + j;
}

What the hell is wrong with this code?
"Can not implicitly convert type int to short" is the compile error.
ASP.NET 1.1 C#

Thanks

Feb 6 '06 #3
To me this is an inconsistency in the framework;

Int32 + Int32 can be longer than an Int32 (int), but it does this silently
(wrapping), returning an Int32.
Likewise with Int64 (long), returning an Int64; yet Int16 (short) returns an
Int32 on summation.

*Presumably* this was for some some meaningful reason... not quite sure what
it is though; anybody?

Marc
Feb 6 '06 #4
Marc Gravell wrote:
To me this is an inconsistency in the framework;

Int32 + Int32 can be longer than an Int32 (int), but it does this silently
(wrapping), returning an Int32.
Likewise with Int64 (long), returning an Int64; yet Int16 (short) returns an
Int32 on summation.

*Presumably* this was for some some meaningful reason... not quite sure what
it is though; anybody?


Performance. Processors are generally happier doing Int32+Int32 than
Int16+Int16, so unless you specify that you want to bound it, the
language assumes it's okay to do things the fast way.

Jon

Feb 6 '06 #5
There we are; and yes, that does seem like a pretty good reason to me ;-p
(not that I work with Int16 very often...)

Cheers,

Marc
Feb 6 '06 #6
Thanks for the response guys.
If its a processor performance thing as Jon suggested, then I am
completely ok with it.
If its anything other, then fooey on C#.

Perhaps it is important for me to pose this question as well then.
The reason I went for a (short) is because I figured it is the ideal
data type because my values won't go over 20.
Basically, its a mini chess application and I have an array declared:
short[,] Board;
Given Jon's statement about CPU performance, would I be better to have
declared as an int instead?
There won't be many arithmetic operations, mostly just comparison. The
arithmetic comes in when checking if moves are valid and so on.
It all works fine currently as a short. Whats the recommondation?
Preserve current structure as short, or do a Find/Replace with int ?
(Note, if you think there is even a minor performance improvement
either way, even if its not going to be noticeable, please indicate
so!)

Thanks

Feb 6 '06 #7
Steven Nagy <le*********@hotmail.com> wrote:
Thanks for the response guys.
If its a processor performance thing as Jon suggested, then I am
completely ok with it.
If its anything other, then fooey on C#.

Perhaps it is important for me to pose this question as well then.
The reason I went for a (short) is because I figured it is the ideal
data type because my values won't go over 20.
Why not use byte then, out of interest?
Basically, its a mini chess application and I have an array declared:
short[,] Board;
Given Jon's statement about CPU performance, would I be better to have
declared as an int instead?
I doubt that it'll make a significant difference for you - but if you
have a *lot* of boards in memory at a time, then the memory difference
could be significant.
There won't be many arithmetic operations, mostly just comparison. The
arithmetic comes in when checking if moves are valid and so on.
It all works fine currently as a short. Whats the recommondation?
Preserve current structure as short, or do a Find/Replace with int ?
(Note, if you think there is even a minor performance improvement
either way, even if its not going to be noticeable, please indicate
so!)


I'd consider going to byte, but wouldn't move to int without
benchmarking first. You *may* find a minor performance improvement, but
I doubt it personally.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 6 '06 #8
Ok thanks Jon.
yeah I don't know why I didn't go byte, perhaps because I am mental.

Yes, there could be lots of boards in memory at once in the future,
which is what I am catering for now.

In the trade off of faster cpu vs less ram, I guess I have the dilemna
of:

If I go for CPU, the request can be completed quicker, and then garbage
collection returns my ram.
If I go for RAM, then I can handle more simultaneous requests, but they
process slower.

Given a web site context (ASP.NET) then which should I choose?
Process the requests faster, or preserve as much ram as possible?

Feb 7 '06 #9
Steven Nagy <le*********@hotmail.com> wrote:
Ok thanks Jon.
yeah I don't know why I didn't go byte, perhaps because I am mental.

Yes, there could be lots of boards in memory at once in the future,
which is what I am catering for now.

In the trade off of faster cpu vs less ram, I guess I have the dilemna
of:

If I go for CPU, the request can be completed quicker, and then garbage
collection returns my ram.
If I go for RAM, then I can handle more simultaneous requests, but they
process slower.

Given a web site context (ASP.NET) then which should I choose?
Process the requests faster, or preserve as much ram as possible?


I doubt whether the difference in either will be signficant, to be
honest - but you won't know without benchmarking (as close to real
usage as possible).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 7 '06 #10
Ok cheers mate

Feb 7 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

34 posts views Thread by Nikola Skoric | last post: by
5 posts views Thread by raz | last post: by
4 posts views Thread by merlevo | last post: by
2 posts views Thread by Lampa Dario | last post: by
29 posts views Thread by Ark | last post: by
6 posts views Thread by Malvolio | last post: by
5 posts views Thread by Puppet_Sock | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.