VB.NET ULong overflow - vb.net

I have a timer which returns the time elapsed:
Public Function TimeElapsed() As ULong
Dim ul As ULong
ul = m_lEnd - m_lStart
Dim ul2 As ULong
ul2 = ul - m_lOverhead
Dim ul3 As ULong
ul3 = (ul2 / m_lFreq) * 1000
Return ul3
End Function
Now I experienced the following variables:
m_lEnd = 935083366402
m_lStart = 935007142800
ul2 = 76223588
m_lOverhead = 14
This gives me an overflow in the line
ul3 = (ul2 / m_lFreq) * 1000
I can not see why and how to improve my bug.
Thank you very much for the help!

You need to explicitly tell the compiler you are doing the operation on a ULong... This is because the numeric literals default to Integers, so your trying to use an integer literal which will cause an overflow like your issue. You can force values to different types with the correct suffix. Literal values without a type suffix may only have values up to the range of Long. This case it's ULong so the suffix is: UL.
By the way this is a conversion error, turn Option Strict on...
EX: Dim m_lEnd As ULong = 935083366402UL
Notice this at the end, this is what needs to happen.

Related

How to store a big number in a variable

Nothing works, tried ULong, integer, uint64, biginteger, decimal. how to store this number in the variable? Option strict on
error: Overflow
Dim Number_N As Integer = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As ULong = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As UInt64 = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As BigInteger = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As Decimal = 115792089237316195423570985008687907853269984665640564039457584007908834671663
You would use BigInteger but you can't represent a BigInteger as a literal as you can with other numeric data types because it's not part of the language. You would have to represent the number in a String literal and call BigInteger.Parse:
Dim Number_N = BigInteger.Parse("115792089237316195423570985008687907853269984665640564039457584007908834671663")

Why do I get an overflow when doing a logical AND

Am using VS2010 VB.net
Dim Register As UInt64
Register = 12297264199100303880
If (Register And &H3FFF) = &H555 Then ' Get Overflow exception here
MsgBox("Done")
End If
Why is this happening and is there a work-around?
Your literal values are implicitly typed as long (Int64), since you didn't specify a type for them. I actually got the overflow on the assignment to Register, since the value given is too big for a long. To get this to work, just specify the type for your literal values, e.g. UL for unsigned long:
Dim Register As UInt64
Register = 12297264199100303880UL
If (Register And &H3FFFUL) = &H555UL Then
MsgBox("Done")
End If
Turning Option Strict On would be helpful, in this case. If you did, you would immediately see the problem. The problem is that the literals are interpreted as Integer (Int32) rather than ULong (UInt64). In order to force the literal to be interpreted as a ULong values, you need to add the UL type suffix:
Dim Register As UInt64
Register = 12297264199100303880UL
If (Register And &H3FFFUL) = &H555 Then ' Get Overflow exception here
MsgBox("Done")
End If
I fixed this by:
Dim DoneMask As UInt64 = &H3FFF
Dim Register As UInt64
Register = 12297264199100303880
If (Register And DoneMask) = &H555 Then ' Get Overflow exception here
MsgBox("Done")
End If
Apparently VB does better with explicit data types instead of a literal

Overload resolution failed because no accessible `Sleep` can be called with these arguments

I have the following code:
Dim MyFile As String
MyFile = ("C:\Book1.xlsx")
Dim infoReader As System.IO.FileInfo
infoReader = My.Computer.FileSystem.GetFileInfo(MyFile)
Threading.Thread.Sleep(infoReader.Length / 1000)
How can I solve the following error:
You need to convert your number (double) to a timeSpan:
Dim sleepTime As Timespan = TimeSpan.FromSeconds(infoReader.Lenth/1000)
Divide it using the integer division. The sleep function doesn't take a double as parameter.
Threading.Thread.Sleep(infoReader.Length \ 1000)
The result a division operation (infoReader.Length / 1000) is a floating point number (Double in our case since FileInfo.Length property is of type Long).
On the other hand, the Threading.Thread.Sleep() overload you're most likely wanting to use expects an Int parameter.
Cast the division result to Int explicitly (is the files are not too big):
Threading.Thread.Sleep(CInt(infoReader.Length / 1000))
Or use the second overload accepting TimeSpan, using the FromMilliseconds method which accepts Double:
Threading.Thread.Sleep(TimeSpan.FromMilliseconds(infoReader.Length / 1000))

Visual Basic.Net Converting a Long to a Ulong

I am having issues using VB.Net trying to convert a Long to a ULong.
I have tried many combinations and keep getting overflow errors.
I have a signed value of -2147483648, I know it will have a Ulong of 2151196588 once converted.
However I want to do this in a programmatic fashion because I am parsing values that can be negative and positive, but when the numbers are negative, they need to have the proper ulong value.
Note: Absolute values wont work, it needs to be a ulong for a true value.
Thank you.
If you mean reinterpret_cast<ulong>(long), then use the same technique:
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _
Public Structure LongULongUnion
<Runtime.InteropServices.FieldOffset(0)> Public l As Long
<Runtime.InteropServices.FieldOffset(0)> Public ul As ULong
End Structure
Sub Main()
Dim u As LongULongUnion
u.l = -2147483648L
Console.WriteLine(u.ul)
Console.ReadLine()
End Sub
But that gives 18446744071562067968 when converted.
You can use the BitConverter class:
Dim a As Int64 = -2147483648
Dim b = BitConverter.ToUInt64(BitConverter.GetBytes(a), 0)
Console.WriteLine(b.ToString) ' outputs: 18446744071562067968
Are you sure your intended value of 2151196588 is correct?
This is the easiest way to convert any Long to ULong:
Dim x As Long = -2147483648
Dim y As ULong = Not (CType((Not x), ULong)) ' = 18446744071562067968
I'm a C# programmer, so please correct me if I converted it to VB.NET incorrectly. My C# code was:
long x = -2147483648;
ulong y = ~((ulong)~x); // = 18446744071562067968
Basically you take the complement of the negative value, which is always a positive value. You can then safely cast it to ULong. Take the complement again and you have the same bit pattern cast to ULong.
Make sure you are converting your value to positive BEFORE you convert it to ulong, negatives are not in a ulong's value scope.
long: -9223372036854775808 to 9223372036854775807
ulong: 0 to 18446744073709551615

How to pass integer as unsigned parameter in VB.NET?

I'm using a library call, setInstance(ByVal instance As UInteger), in my VB.NET code. The parameter I need to pass is an Integer. Is there anything I need to do to convert the integer parameter to an unsigned integer? The number is guaranteed to be positive and less than 10.
Like so...
Dim MyInt As Int32 = 10
Dim MyUInt As UInt32 = CUInt(MyInt)
setInstance(MyUInt)
CUInt or CType(x, UInt) allow converting a positive integer.
It throws an exception when x is negative.
To use Int as Uint, you can use some tricks:
dim bb() = System.BitConverter.GetBytes(myInt)
dim MyUint = System.BitConverter.ToUInt32(bb, 0)
Also with System.Buffer.BlockCopy for arrays.
If you configure the compiler to disable Check Integer Overflow (default for C#). Then you can use CUInt with negative values with no check - not exception.
You can call CUint to convert a variable to a UInteger.