[solved] Subracting two shorts [modified]
-
I have this bit of code:
short s1 = 10;
short s2 = 5;
short s3 = s1 - s2;Error says: Error Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Aren't shorts just 16 bit integers? You can't subtract two of them?
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 9:45 PM
-
I have this bit of code:
short s1 = 10;
short s2 = 5;
short s3 = s1 - s2;Error says: Error Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Aren't shorts just 16 bit integers? You can't subtract two of them?
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 9:45 PM
int was assumed on the right side of the assignment. MSDN
-
I have this bit of code:
short s1 = 10;
short s2 = 5;
short s3 = s1 - s2;Error says: Error Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Aren't shorts just 16 bit integers? You can't subtract two of them?
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 9:45 PM
For some reason the designers of C# decided that 8- and 16-bit operations should return a 32-bit result rather than the expected data type :mad: .
-
For some reason the designers of C# decided that 8- and 16-bit operations should return a 32-bit result rather than the expected data type :mad: .
Thanks to both. That makes very little sense to me. If anyone knows a good *why* answer, I would love to hear it, but I will just use a cast. Thanks,
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
-
Thanks to both. That makes very little sense to me. If anyone knows a good *why* answer, I would love to hear it, but I will just use a cast. Thanks,
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
All integer expressions are computed using ints or larger, even if all the terms in it are bytes or shorts; that has been true in all C-like languages. The difference with C# is it complains when trying to store such expression result in anything smaller than int; C and C++ silently truncated the result, with a possible value error for free. In C# you need a cast, so you know you are responsible for a possible truncation. try this (or its equivalent) in different languages:
ushort a=1; ushort b=0xFFFF; a++; a=(ushort)(a+1); b++; b=(ushort)(b+1);
You may not like it, but this is how it has been defined. The details can be found in section 7.2.6.2 of the C# language specification. If you were to study the programming language you choose from a book, you would probably know such things. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig:
-
All integer expressions are computed using ints or larger, even if all the terms in it are bytes or shorts; that has been true in all C-like languages. The difference with C# is it complains when trying to store such expression result in anything smaller than int; C and C++ silently truncated the result, with a possible value error for free. In C# you need a cast, so you know you are responsible for a possible truncation. try this (or its equivalent) in different languages:
ushort a=1; ushort b=0xFFFF; a++; a=(ushort)(a+1); b++; b=(ushort)(b+1);
You may not like it, but this is how it has been defined. The details can be found in section 7.2.6.2 of the C# language specification. If you were to study the programming language you choose from a book, you would probably know such things. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig:
Luc, Thanks for your explanation. Not sure if the last line is meant condescendingly, but I value your input too much to complain ;-) . And your right. Its been a while since I really dug through my c# books. This is a PT thing for me. When it was FT, I did spend a lot more time in the books. Thanks again.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
-
Luc, Thanks for your explanation. Not sure if the last line is meant condescendingly, but I value your input too much to complain ;-) . And your right. Its been a while since I really dug through my c# books. This is a PT thing for me. When it was FT, I did spend a lot more time in the books. Thanks again.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
Dwayner79 wrote:
Not sure if the last line is meant condescendingly
Not at all. Too many people try and learn a language by experimenting, or reading a few things on the web, instead of really studying a book. A book tends to teach such stuff in a logic order, explaining how and why things are the way they are, and IMO there isn't really any alternative. And that's what I wanted to suggest. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig: