www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - convert int to short

reply James <james gmail.com> writes:
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.)
Oct 29 2008
next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
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.
Oct 29 2008
prev sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
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.
Oct 29 2008