The following code in C# gives result "228452386"
UInt32 a;
int k = 0;
a = 0x9E3779B9;
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
After above code is ran, "a" contains "228452386".
But the following same code in VB.NET results in "Arithmetic operation resulted in an overflow". Basically the last statement is returning value "1868983913", so the runtime error is generated.
Dim a As UInt32
Dim k As Integer = 0
a = &H9E3779B9UI
a += CUInt(AscW(url(k + 0)) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24))
Please note the variable "url" in the above code could be any string and it is same for both codes.
When I run the following statements in both C# and VB.NET than they both return same value
C#
(UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24))
VB.NET
CUInt(AscW(url(k + 0)) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24))
Both statements return the value "1868983913" for "url" "info:microsoft.com". But when I run the complete statement with a += ......... then VB.NET gives an error while C# returns the value "228452386".
You have an overflow, because the result of adding the two numbers you're adding are larger than what fits in an UInt.
The resulting number is a 33 bit number, UInts fit only 32 bits.
The VB.NET compiler (or VS) allows you to turn of this overflow checking, so that VB.NET will behave like C# in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With