-1

This is one of the lines I am having in C# app override of WndProc:

int hiWord = unchecked((short)((long)m.LParam >> 16)); 
if (hiWord != 0)
{
   // do something
}

Is it possible that it may throw Arithmetic exception?

Leon Havin
  • 177
  • 2
  • 14

1 Answers1

1

No, it won't throw exceptions, but you need to be fully aware of arithmetic details of right shift operator; that the highest order sign bit will be kept (if LParam is singed value), and the way long value will be truncated into short; that highest 48 order bits will be removed and the rest 16 bits will be returned only.

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
  • Can you explain why casting short to long will create overflow? – Leon Havin Jan 29 '17 at 21:42
  • @LeonHavin There is no overflow in casting from long to short, it is a truncation. I edited my answer and removed the word overflow, as I just was trying to point at the right shift operator `>>`'s sign bit. – Ghasan غسان Jan 30 '17 at 01:42