473,322 Members | 1,347 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,322 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 1442
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Nikola Skoric | last post by:
Is the a PHP compiler? A program that compiles PHP code to executable which doesn't need php interpreter to execute... -- Pozdrav/Regards, Nikola Skoric. "...Usne, tice-rugalice - a u oku...
5
by: raz | last post by:
Greetings all. I apologize for what is almost certainly a stupid question, but I can't figure this out, and have no more time for head bashing... The short version: what is the appropriate...
4
by: merlevo | last post by:
I am trying to set up the intel compiler. I have no trouble when I #include <stdio.h> but if I #include (iostream> using namespace std; I get an error "could not open source file "iostream" ...
2
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int...
16
by: pj | last post by:
(Was originally, probably wrongly, posted to the vc subgroup.) (This doesn't appear to be a c# problem, but a problem with a bug in the Visual Studio c# compiler, but, any help will be welcome...)...
3
by: Bill | last post by:
I am confused why I must cast an enum to a type int if I defined the enum as follows: enum COLUMNS :int { IP=0, MSG =1 } IMHO the compiler should be able to make the conversion without...
29
by: Ark | last post by:
A function int foo(struct T *x) { return (x+1)-x; } should always return a 1, no matter how T is defined. (And int could be replaced with ptrdiff_t for you pedants.) For one thing, it was...
6
by: Malvolio | last post by:
I know this is a *really* stupid question but what the hell is visual c++? I downloaded it and it doesn't seem to work. It isn't like any c++ compiler I ever saw before and the manual on the...
5
by: Puppet_Sock | last post by:
So, I'm madly coding away, and my fingers stutter, and I produce this. (mfirstToken is a std::string object.) if(m_firstToken.c_str() == 'M' || m_firstToken.c_str().c_str() == 'T') { // ......
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.