473,320 Members | 1,900 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,320 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 7905
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
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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

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.