Strange behavior of CInt function: throws System.OverflowException

  Kiến thức lập trình

The maximum value for the Integer data type is 2’147’483’647.
When I run this code:

Dim t As Integer = 4094
Dim sz as Integer
sz = CInt((t / 2) * 1024 * 1024)
sz = CInt(t * 1024 * 1024 / 2)

line 3 executes well, but line 4 throws System.OverflowException: ‘Arithmetic operation resulted in an overflow.’ Despite the fact that in both cases arithmetic expression results in the same value: 2’146’435’072, which is less than the maximum value for the Integer data type.
How can this be explained?

LEAVE A COMMENT