469,288 Members | 2,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

How to cast int to short

ad
I have a custom function which accept short as parameter.
for example:

public void myFun(short myShor)
{...}

Now I have a integer (int i), I want to take as the aprameter of myFun,
How can I cast i to short ?
Feb 15 '06 #1
4 29861
ad <fl****@wfes.tcc.edu.tw> wrote:
I have a custom function which accept short as parameter.
for example:

public void myFun(short myShor)
{...}

Now I have a integer (int i), I want to take as the aprameter of myFun,
How can I cast i to short ?


myFun ((short)i);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 15 '06 #2
Jon Skeet [C# MVP] wrote:
ad <fl****@wfes.tcc.edu.tw> wrote:
public void myFun(short myShor) {...}

Now I have a integer (int i), I want to take as the aprameter of
myFun, How can I cast i to short ?


myFun ((short)i);


Note that, by default, C# checks for integer overflow when performing a cast
in this obvious way. If you prefer to truncate the most significant bits,
you either need to remove them beforehand using a bitmask or perform the
cast inside an "unchecked {}" block. You can read more about unchecked here:

http://msdn.microsoft.com/library/de...funchecked.asp

--
Derrick Coetzee, MCAD, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
Feb 16 '06 #3
Derrick Coetzee [MSFT] <dc******@online.microsoft.com> wrote:
myFun ((short)i);


Note that, by default, C# checks for integer overflow when performing a cast
in this obvious way. If you prefer to truncate the most significant bits,
you either need to remove them beforehand using a bitmask or perform the
cast inside an "unchecked {}" block. You can read more about unchecked here:

http://msdn.microsoft.com/library/de...ary/en-us/csre
f/html/vclrfunchecked.asp


No, by default C# *doesn't* check for overflow, except for compile-time
constant expressions. So:

// Doesn't compile
uint x = (uint)-1;
// Does compile
uint x = unchecked((uint)-1);

// Compiles and runs without exception
int y = -1;
uint x = (uint)y;

// Compiles but throws an exception
int y = -1;
uint x = checked((uint)y);

Note that the default behaviour can also be controlled by a compiler
switch or project setting.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '06 #4
Jon Skeet [C# MVP] wrote:
Derrick Coetzee [MSFT] <dc******@online.microsoft.com> wrote:
myFun ((short)i);


Note that, by default, C# checks for integer overflow when
performing a cast in this obvious way. [...]


No, by default C# *doesn't* check for overflow, except for
compile-time constant expressions.


Sorry, my mistake - I'm used to this option being on. I do personally
recommend that the /checked option (Build->Advanced->Check for arithmetic
overflow/underflow) is always used, as this can help to find bugs and
prevent potential security issues, as well as provide conceptual safety
guarantees (and I think it should be on by default). If profiling reveals
that they're slowing down a bottleneck, or if you want the C behaviour of
arithmetic mod 2^word size, you can always wrap the relevant code in
unchecked { }. Thanks for the correction, Jon.
--
Derrick Coetzee, MCAD, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
Feb 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Michael Baehr | last post: by
6 posts views Thread by JS | last post: by
1 post views Thread by C Learner | last post: by
7 posts views Thread by simon | last post: by
6 posts views Thread by Jack | last post: by
9 posts views Thread by Hamish M | last post: by
4 posts views Thread by Terry | last post: by
8 posts views Thread by ManicQin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.