473,320 Members | 1,961 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.

the pains of casting ushort + ushort...

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 questions:

1 - Why? Why would they do this, this way? Why not automatically cast
it to the size of the recipient variable? I have not benchmarked
this, but I cant imagine using many of these casts all over the place -
in order to keep memory usage low - would be faster than the compiler
doing this automatically. And frankly, its very ugly. I can see lazy
developers using ints wherever they want since its quicker. (and then
using unnecessary validation code to check their values).

2 - Would it be possible to write up some operator overloads that
handle this, and create a custom type? Sure, you're still stuck with
the casting on your own, but the effect would be cleaner and more
readable.

Thanks for the replies in advance

Dec 29 '06 #1
1 1992
xlar54 wrote:
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 questions:

1 - Why? Why would they do this, this way? Why not automatically cast
it to the size of the recipient variable? I have not benchmarked
this, but I cant imagine using many of these casts all over the place -
in order to keep memory usage low - would be faster than the compiler
doing this automatically. And frankly, its very ugly. I can see lazy
developers using ints wherever they want since its quicker. (and then
using unnecessary validation code to check their values).
Two guesses:

1) the result of adding two ushort is not necesarrily a ushort

2) programming tradition tradition
2 - Would it be possible to write up some operator overloads that
handle this, and create a custom type? Sure, you're still stuck with
the casting on your own, but the effect would be cleaner and more
readable.
No you can not overload ushort+ushort.

The operator overload must be defined in the class itself.

I guess you could create a wrapper class. But performance would
probably suffer.

Arne

Dec 30 '06 #2

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

Similar topics

10
by: Axel Dahmen | last post by:
Hi, I want to start a technical discussion on the fact that C# doesn't define any mathematical operators on other integral types than int. Simple things like the following just aren't possible...
4
by: JuLiE Dxer | last post by:
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...
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, --...
4
by: kelli | last post by:
i am new to c# so if this is a trivial problem, forgive me! i've searched the web and after 2 days still cannot solve it. i am converting a c++ application to c# - the problem code is listed...
2
by: AMP | last post by:
Hello, I have: ushort length= 4; ushort len; And I get an error here: length = len + 4; Error 6 Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you...
6
by: AMP | last post by:
Hello, First, Thanks for ALL your help. Second, another question: I have some c code i am turning into c#:(truncated) WORD calcChecksum(BYTE data, WORD length) /* Calculates a checksum of...
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...
12
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
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: 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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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....

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.