473,231 Members | 1,659 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,231 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 1438
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.