Sunday, December 13, 2015

Casting part 2

Hi All...!

Today I'm going to teach you guys how to cast "int" data type variables in to "short". Keep it in mind we do casting to convert large capacity data type's variables to smaller capacity data type's variables.

Let's cast "int" to "short"



Let's see what will happen when int value exceed range of short value (short value range is -32768 to 32767 short gets this range because short is 16 bit data type and 2^16  )


32767 is inside the short range, Let's do the same coding with the 32768 and find out what will happen.


Answer is -32768 we got this value because 2^16 is 65536 and 32768 - 65536  = -32768.

Like this if we cross the short range from minus side we will get,



-32769 + 65536 = 32767.

No comments:

Post a Comment