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

Does (ushort + ushort) return an int value?


Perhaps, I skipped over learning this particular tidbit.

I ran across an odd error while trying to add to ushort variables
together and assigning it to a 3rd ushort variable.

I get this compiler error:

error CS0029: Cannot implicitly convert type 'int' to 'ushort'
code is basically this
ushort u1, u2, u3;

u1 = 3;
u2 = 5;

u3 = u1 + u2;

Does ushort math always an int object?

Nov 16 '05 #1
4 7922
JuLiE Dxer <Ms*****@verizon.net> wrote:

Perhaps, I skipped over learning this particular tidbit.

I ran across an odd error while trying to add to ushort variables
together and assigning it to a 3rd ushort variable.

I get this compiler error:

error CS0029: Cannot implicitly convert type 'int' to 'ushort'

code is basically this

ushort u1, u2, u3;

u1 = 3;
u2 = 5;

u3 = u1 + u2;

Does ushort math always an int object?


Sort of. In fact, you can't do maths with ushorts. What you *can* do
maths on is ints, and that's what happens with the above. You've
effectively got:

u3 = (int)u1 + (int)u2;

The only integer addition operators defined are:

int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);

The normal (and somewhat long-winded) operator overload resolution is
applied to select the first of these as the best overload, and that's
the one which is used.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
try
u1 = (ushort)1;
u2 = (ushort)2;

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #3
pontifikas wrote:
try
u1 = (ushort)1;
u2 = (ushort)2;


Nope, see Jon Skeet's answer as to why that won't work. The following
would work:

ushort u1, u2, u3;
u1 = 3;
u2 = 5;

u3 = (ushort)(u1 + u2);
--
Tom Porterfield
Nov 16 '05 #4
Interesting..

Thanks for insight.
Nov 16 '05 #5

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

Similar topics

8
by: Sivas | last post by:
Hi, Can anyone tell me why this does not work: --------------------------------------------- float b = 2.51F; switch(b) {
2
by: Drasko | last post by:
I am using HybridDictionry in my app, so it works fine if key is a string. When I tried to make my class with ushort key, it throws ArgumentException? First time when I add item it is ok, but when...
3
by: Peter Aitken | last post by:
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type ushort. I though I would use the complement operator ~ but the compiler won't buy it. How can I do this? Thanks, --...
2
by: Marcelo Muzilli | last post by:
Howdy all, in my program, I have a ushort variable and a string variable and if I try to compile it, I'm receiving this error message: "Cannot convert type 'string' to 'ushort'". How can I...
4
by: rakesh.Mysore | last post by:
HI I have a problem in casting short to ushort in my project. i have a ushort value of 38,143 now it is converted into short value of 27,393 and stored in variable.(when i casted ushort to...
1
by: dast | last post by:
I would need a function to copy unsigned short values from a IntPtr to a ushort-array, but the Marshal.Copy-function only support the short-array! At the moment I do it the following way: ......
1
by: xlar54 | last post by:
I have some code that ends up looking alot like: ushort x = (ushort)(m + b); whereas both m and b are ushorts also. This is due to the automatic casting of values to int. Some quick...
22
by: Yakov | last post by:
What would be the nicest way to write the loop for all values of unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ? For comparison, there is just one way to write the for(k=0;...
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.