When i do casting from long to short will it consider LSB or MSB?
When casting from a higher data type to a lower type, the basic principle is to make sure that the value is correct, if at all it can be correct. If it cannot be correct, the C compiler does not care how much wrong it is.
Since this cast will be correct only if the higher data type value is withing the range of the lower type, casting always implies retaining the Lower Significant Part ( and NOT the More Significant Part ). Hence the C compiler chops of higher bits in the casting.
long –> short : Chops off the MS 16 bits and retains the LS 16 bits.
long –> char : Chops off the MS 24 bits and retains the Least Significant 8 bits.
MLS Shastry
Please login first to submit.