Thread overview
convert int to short
Oct 29, 2008
James
Oct 29, 2008
Robert Fraser
October 29, 2008
can anybody tell me how to convert int value to short since normal type-casting would return diff value (im not familiar with bit-shift, etc.)
October 29, 2008
James wrote:
> can anybody tell me how to convert int value to short since normal type-casting would return diff value (im not familiar with bit-shift, etc.)

Assuming the int can fit in a short (that is, it is between -32768 and 32767, inclusive) just use cast(short). Otherwise... well, that's for you to figure out depending on the needs of your application.
October 29, 2008
On Wed, Oct 29, 2008 at 4:17 AM, James <james@gmail.com> wrote:
> can anybody tell me how to convert int value to short since normal type-casting would return diff value (im not familiar with bit-shift, etc.)
>

It's... it's called the pigeonhole principle.  You have too many things to put into too few slots.  A short can only hold one of 65,536 distinct values, whereas an int can hold over 4 billion.  You just can't put 4 billion things into 65,536 slots.  So yes, for all but numbers in the range that a short can hold, casting will give you a different value.