UInt64 accepting only half it's capacity

Joined
Mar 14, 2005
Messages
10
Programming Experience
5-10
I'm using VB.net and the .framework version 2.0

I am puzzled why I can only assign values for UInt64 and ULong variables that are one-half the advertised 64 bit capacity. For signed integer values I know that one 1 bit represents the sign of the value but both of these variables are unsigned.

On my computer both variables accept the Hex 7FFFFFFFFFFFFFFF value but gives me a "Constant expression not representable in type 'ULong'. " when I try to assign the value of Hex 800000000000000 despite the fact that the max value is Hex FFFFFFFFFFFFFFFF

Public Sub Test()

Dim uint64Var As UInt64
Dim ulongVar As ULong

uint64Var = UInt64.MaxValue
uint64Var = UInt64.MinValue
uint64Var = &H7FFFFFFFFFFFFFFF
uint64Var = &H8000000000000000

ulongVar = ULong.MaxValue
ulongVar = ULong.MinValue
ulongVar = &H7FFFFFFFFFFFFFFF
ulongVar = &H8000000000000000

End Sub
 
Have you tried with a 64-bit Operating System and the x64 edition of .Net Framework 2.0?
 
Your problem is not the variables. It's the literals that you're assigning to them. Without a suffix the values you are specifying will be of type Long, and &H8000000000000000 is not a valid Long value. Use the "UL" suffix to force those literal values to type ULong.
VB.NET:
 uint64Var = UInt64.MaxValue
uint64Var = UInt64.MinValue
uint64Var = &H7FFFFFFFFFFFFFFFUL
uint64Var = &H8000000000000000UL
 
ulongVar = ULong.MaxValue
ulongVar = ULong.MinValue
ulongVar = &H7FFFFFFFFFFFFFFFUL
ulongVar = &H8000000000000000UL
The literals that are within the range of the Long type are OK as they will be implicitly converted to ULong, but it is good practice to use literals of the same type as the variables you are assigning to.
VB.NET:
        Dim s1 As Short = 0S
        Dim us As Short = 0US
        Dim i1 As Integer = 0 'No suffix needed as Integer is the default type.
        Dim i2 As Integer = 0I 'You can use a suffix if you like.
        Dim ui As UInteger = 0UI
        Dim l1 As Long = 2147483648 'No suffix necessary as Long is the default type for values greater than Integer.MaxValue.
        Dim l2 As Long = 0L 'You can use a suffix if you like.
        Dim ul As ULong = 0UL
        Dim s2 As Single = 0.0F
        Dim d1 As Double = 0.0 'No suffix needed as Double is the default type for numbers with a decimal point
        Dim d2 As Double = 0.0R 'You can use a suffix if you like.
        Dim d3 As Decimal = 0D
 
Back
Top