473,378 Members | 1,417 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,378 software developers and data experts.

Problems with short and + operator

I wrote this:
short s1 = 0;
short s2 = 1;
short s3 = s1 + s2;

And gor this compile error message: Cannot implicitly convert type 'int' to
'short'

What is wrong here?

Regards
Anders
Nov 15 '05 #1
8 1660
NilsNilsson <an************@hotmail.com> wrote:
I wrote this:
short s1 = 0;
short s2 = 1;
short s3 = s1 + s2;

And gor this compile error message: Cannot implicitly convert type 'int' to
'short'

What is wrong here?


Any operation involving two shorts has a type of int. In other words,
you need:

short s3 = (short) (s1+s2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Thanks,
it works!

But I still think this is strange feature i C#?

/Anders

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
NilsNilsson <an************@hotmail.com> wrote:
I wrote this:
short s1 = 0;
short s2 = 1;
short s3 = s1 + s2;

And gor this compile error message: Cannot implicitly convert type 'int' to 'short'

What is wrong here?


Any operation involving two shorts has a type of int. In other words,
you need:

short s3 = (short) (s1+s2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Thus spake NilsNilsson:
Thanks,
it works!

But I still think this is strange feature i C#?


It's a safeguard against overflow errors. Your operation may produce a
result that exceeds the storage capacity of the short data type.

BTW, the help on the short data type actually states that your original
code will produce an error.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #4
Thus spake NilsNilsson:
Thanks,
it works!

But I still think this is strange feature i C#?


It's a safeguard against overflow errors. Your operation may produce a
result that exceeds the storage capacity of the short data type.

BTW, the help on the short data type actually states that your original
code will produce an error.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #5
This is not really specific to C#, you will get the same thing in Java.

Bruno.

"NilsNilsson" <an************@hotmail.com> a écrit dans le message de
news:u9*************@TK2MSFTNGP09.phx.gbl...
Thanks,
it works!

But I still think this is strange feature i C#?

/Anders

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
NilsNilsson <an************@hotmail.com> wrote:
I wrote this:
short s1 = 0;
short s2 = 1;
short s3 = s1 + s2;

And gor this compile error message: Cannot implicitly convert type
'int'
to 'short'

What is wrong here?


Any operation involving two shorts has a type of int. In other words,
you need:

short s3 = (short) (s1+s2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #6
This is not really specific to C#, you will get the same thing in Java.

Bruno.

"NilsNilsson" <an************@hotmail.com> a écrit dans le message de
news:u9*************@TK2MSFTNGP09.phx.gbl...
Thanks,
it works!

But I still think this is strange feature i C#?

/Anders

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
NilsNilsson <an************@hotmail.com> wrote:
I wrote this:
short s1 = 0;
short s2 = 1;
short s3 = s1 + s2;

And gor this compile error message: Cannot implicitly convert type
'int'
to 'short'

What is wrong here?


Any operation involving two shorts has a type of int. In other words,
you need:

short s3 = (short) (s1+s2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #7
The reason this is the case is because the CLR evaluation stack uses 4-byte
and 8-byte integers, so the short will get widened when it is pushed on the
stack, and then the add instruction comes up with an Int32 result.

The compiler could generate code to automatically cast this back to short
for you, however this would be hiding some of the details of what was
actually going on, would be limiting functionality, and would be prone to
errors. Consider the following method:

public void Method()
{
checked {
short a = 0x7fff;
short b = 0x7fff;
int c = a + b;
}
}

The method will work with the current system. Suppose, however, that the
compiler forced a cast back to short behind the scenes. Then the last line
would change to something like:

int c = (int)(short)(a + b); // where the cast to short is added by
the hypothetical compiler

Not only would you get an invalid result due to overflow errors on the cast
to short, but in this case it would always throw an exception because it is
in a checked context.

-Mark

--
Mark Andersen, Visual C# Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

--------------------
From: "NilsNilsson" <an************@hotmail.com>
References: <u0**************@TK2MSFTNGP10.phx.gbl> <MP************************@news.microsoft.com>Subject: Re: Problems with short and + operator
Date: Wed, 24 Sep 2003 10:17:56 +0200
Lines: 32
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <u9*************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: server2.triona.se 217.31.172.231
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186980
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Thanks,
it works!

But I still think this is strange feature i C#?

/Anders

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.co m...
NilsNilsson <an************@hotmail.com> wrote:
> I wrote this:
> short s1 = 0;
> short s2 = 1;
> short s3 = s1 + s2;
>
> And gor this compile error message: Cannot implicitly convert type
'int'
to > 'short'
>
> What is wrong here?


Any operation involving two shorts has a type of int. In other words,
you need:

short s3 = (short) (s1+s2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #8
The reason this is the case is because the CLR evaluation stack uses 4-byte
and 8-byte integers, so the short will get widened when it is pushed on the
stack, and then the add instruction comes up with an Int32 result.

The compiler could generate code to automatically cast this back to short
for you, however this would be hiding some of the details of what was
actually going on, would be limiting functionality, and would be prone to
errors. Consider the following method:

public void Method()
{
checked {
short a = 0x7fff;
short b = 0x7fff;
int c = a + b;
}
}

The method will work with the current system. Suppose, however, that the
compiler forced a cast back to short behind the scenes. Then the last line
would change to something like:

int c = (int)(short)(a + b); // where the cast to short is added by
the hypothetical compiler

Not only would you get an invalid result due to overflow errors on the cast
to short, but in this case it would always throw an exception because it is
in a checked context.

-Mark

--
Mark Andersen, Visual C# Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

--------------------
From: "NilsNilsson" <an************@hotmail.com>
References: <u0**************@TK2MSFTNGP10.phx.gbl> <MP************************@news.microsoft.com>Subject: Re: Problems with short and + operator
Date: Wed, 24 Sep 2003 10:17:56 +0200
Lines: 32
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <u9*************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: server2.triona.se 217.31.172.231
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186980
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Thanks,
it works!

But I still think this is strange feature i C#?

/Anders

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.co m...
NilsNilsson <an************@hotmail.com> wrote:
> I wrote this:
> short s1 = 0;
> short s2 = 1;
> short s3 = s1 + s2;
>
> And gor this compile error message: Cannot implicitly convert type
'int'
to > 'short'
>
> What is wrong here?


Any operation involving two shorts has a type of int. In other words,
you need:

short s3 = (short) (s1+s2);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
10
by: nyhetsgrupper | last post by:
The following code result in the following compilation error: Error 1 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) short a = 1; short...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.