ulong vs UInt64

  • Thread starter Thread starter Sanjib Biswas
  • Start date Start date
S

Sanjib Biswas

Could anyone point out to me the difference between ulong (C#) and
UInt64/ULong (VB.Net)? I was under the impression that both are same. Below
C# code works fine for a particular value of 'data' (string length 36) and
initial hash value '0' whereas VB.Net code throws an exception in the 8th
iteration, saying the data is either too large or too small for a Uint64. I
am using VB.Net/C# 2005. Am I missing anything here?

C# Code:
=======
static private ulong GetULongHash( string data, ulong hash )
{
hash += ( hash << 13 ) + (ulong)data.Length;

for (int i = 0; i < data.Length; i++)
{
hash += (hash << 17) + data;
}

return hash;
}

VB.Net Code:
==========

Private Shared Function GetUInt64Hash(ByVal data As String, ByVal hash As
System.UInt64) As System.UInt64

hash += (hash << 13) + CType(data.Length, System.UInt64)
Dim i As Integer = 0

While i < data.Length
hash += (hash << 17) + Asc(data(i))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While

Return hash

End Function
 
Sanjib,

There isn't any difference. ULong and ulong are aliases that the VB.NET
and C# compilers use for UInt64.

My guess for the reason you get the exception is because of differences
in the implicit conversions that VB uses vs the implicit conversions that C#
uses. However, without the inputs, it's impossible to tell.

Also, why are you using the Decrement method on the Interlocked class?
There is really no reason to do this.

Hope this helps.
 
Hi Nick,

I added few debug statements in both C# and VB.Net code. Please see the
output.

Data : 17\0x01version="1.0" encoding="utf-8"

VB.Net Code:
============
Length : 36
First Hash : 36
Intermediate Hash : 4718677, 618486431744
data[0] : 49
Intermediate Hash : 618491150476, 81066872075190272
data[1] : 55
Intermediate Hash : 81067490566340748, 353537054712791040
data[2] : 0
Intermediate Hash : 434604545279131908, 941259211282055168
data[3] : 120
Intermediate Hash : 1375863756561187124, 1844235403342118912
data[4] : 48
Intermediate Hash : 3220099159903306085, 3332680371594199040
data[5] : 49
Intermediate Hash : 6552779531497505243, 5514680524283969536
data[6] : 118
Intermediate Hash : 12067460055781474880, 8500575237681709056
data[7] : 101
<Exception> in the 8th iteration

C# Code:
========
Length : 36
First Hash : 36
Intermediate Hash : 4718677, 618486431744
data[0] : 49
Intermediate Hash : 618491150476, 81066872075190272
data[1] : 55
Intermediate Hash : 81067490566340748, 353537054712791040
data[2] : 0
Intermediate Hash : 434604545279131908, 941259211282055168
data[3] : 120
Intermediate Hash : 1375863756561187124, 1844235403342118912
data[4] : 48
Intermediate Hash : 3220099159903306085, 3332680371594199040
data[5] : 49
Intermediate Hash : 6552779531497505243, 5514680524283969536
data[6] : 118
Intermediate Hash : 12067460055781474880, 8500575237681709056
data[7] : 101
Intermediate Hash : 2121291219753632434, 12556076597748432896
data[8] : 114
Intermediate Hash : 14677367817502065445, 17908616609003077632
data[9] : 115
Intermediate Hash : 14139240352795591566, 6368156393674637312
data[10] : 105
Intermediate Hash : 2060652672760677373, 15087140905959424000
data[11] : 111
Intermediate Hash : 17147793578720101483, 7408521081953583104
data[12] : 110
Intermediate Hash : 6109570586964133032, 2028990757499568128
data[13] : 61
Intermediate Hash : 8138561344463701194, 17642991144001601536
data[14] : 34
Intermediate Hash : 7334808414755751163, 17494393418824417280
data[15] : 49
Intermediate Hash : 6382457759870616873, 1659759033328992256
data[16] : 46
Intermediate Hash : 8042216793199609177, 7142914274266054656
data[17] : 48
Intermediate Hash : 15185131067465663867, 15600697893713215488
data[18] : 34
Intermediate Hash : 12339084887469327771, 8694451968501219328
data[19] : 32
Intermediate Hash : 2586792782260995584, 4947481731654483968
data[20] : 101
Intermediate Hash : 7534274513915479662, 4431845962614046720
data[21] : 110
Intermediate Hash : 11966120476529526481, 7374976597178318848
data[22] : 99
Intermediate Hash : 894352999998293824, 14024571425877131264
data[23] : 111
Intermediate Hash : 14918924425875425188, 6156814762711187456
data[24] : 100
Intermediate Hash : 2628995114877061133, 2468400271732637696
data[25] : 105
Intermediate Hash : 5097395386609698939, 3184508020209352704
data[26] : 110
Intermediate Hash : 8281903406819051746, 8541577074476056576
data[27] : 103
Intermediate Hash : 16823480481295108383, 340561220064903168
data[28] : 61
Intermediate Hash : 17164041701360011585, 15706883263652036608
data[29] : 34
Intermediate Hash : 14424180891302496694, 17884414382611103744
data[30] : 117
Intermediate Hash : 13861851200204048938, 6949717196525535232
data[31] : 116
Intermediate Hash : 2364824323020032656, 1612996340124483584
data[32] : 102
Intermediate Hash : 3977820663144516285, 2135460351271632896
data[33] : 45
Intermediate Hash : 6113281014416149237, 8746791831719247872
data[34] : 56
Intermediate Hash : 14860072846135397143, 3101577888347848704
data[35] : 34
Returning Hash : 14860072846135397143

Nicholas Paldino said:
Sanjib,

There isn't any difference. ULong and ulong are aliases that the
VB.NET and C# compilers use for UInt64.

My guess for the reason you get the exception is because of differences
in the implicit conversions that VB uses vs the implicit conversions that
C# uses. However, without the inputs, it's impossible to tell.

Also, why are you using the Decrement method on the Interlocked class?
There is really no reason to do this.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sanjib Biswas said:
Could anyone point out to me the difference between ulong (C#) and
UInt64/ULong (VB.Net)? I was under the impression that both are same.
Below C# code works fine for a particular value of 'data' (string length
36) and initial hash value '0' whereas VB.Net code throws an exception in
the 8th iteration, saying the data is either too large or too small for a
Uint64. I am using VB.Net/C# 2005. Am I missing anything here?

C# Code:
=======
static private ulong GetULongHash( string data, ulong hash )
{ Console.WriteLine("Length : {0}", data.Length);
hash += ( hash << 13 ) + (ulong)data.Length; Console.WriteLine("First Hash : {0}", hash);

for (int i = 0; i < data.Length; i++)
{
hash += (hash << 17) + data;

Console.WriteLine("Intermediate Hash : {0}, {1}", hash, (hash <<
17));
Console.WriteLine("data[{0}] : {1}", i, (0 + data));
 
Hi Greg,

How do I turn off overflow check in VB.Net? I couldn't find this in the
application properties window.

Regards
Sanjib

Greg Young said:
VB.NET is complaining that it would have overflowed ..

C# will not error on this by default.

You can turn this off (disable overflow checks).

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Sanjib Biswas said:
Could anyone point out to me the difference between ulong (C#) and
UInt64/ULong (VB.Net)? I was under the impression that both are same.
Below C# code works fine for a particular value of 'data' (string length
36) and initial hash value '0' whereas VB.Net code throws an exception in
the 8th iteration, saying the data is either too large or too small for a
Uint64. I am using VB.Net/C# 2005. Am I missing anything here?

C# Code:
=======
static private ulong GetULongHash( string data, ulong hash )
{
hash += ( hash << 13 ) + (ulong)data.Length;

for (int i = 0; i < data.Length; i++)
{
hash += (hash << 17) + data;
}

return hash;
}

VB.Net Code:
==========

Private Shared Function GetUInt64Hash(ByVal data As String, ByVal hash As
System.UInt64) As System.UInt64

hash += (hash << 13) + CType(data.Length, System.UInt64)
Dim i As Integer = 0

While i < data.Length
hash += (hash << 17) + Asc(data(i))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While

Return hash

End Function

 
Sanjib Biswas said:
How do I turn off overflow check in VB.Net? I couldn't find this in the
application properties window.

From MSDN:

<quote>
The Visual Basic compiler supports several optimizations that can, in
some cases, make an application file smaller, make an application run
faster, or speed up the build process, Compiler optimizations are set
in the Advanced Compiler Settings dialog box, accessible from the
Compile page of the Project Designer.

To remove integer overflow checks
With a project selected in Solution Explorer, on the Project menu.
click Properties.

Click the Compile tab.

Click the Advanced Compile Options button.

In the Advanced Compiler Settings dialog box, select the Remove integer
overflow checks check box.
</quote>

(Those are instructions for VS.NET 2005.)
 
Unfortunately, I don't see the Advance Compiler Settings. I checked the help
page also
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vsprojopt/html/1f81133a-293f-4dba-bc1c-8baafb01d857.htm.
I am not sure what's causing VB Express edition to not to show this button.
I am using V8.0.50727.42.

Regards
Sanjib
 
Ah express .. See
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=141019&SiteID=1

I will c/p here for aggregators.

<QUOTE>
Then add the following line

<RemoveIntegerChecks>true</RemoveIntegerChecks>

in the containers

<PropertyGroup Condition...Release...">
<PropertyGroup Condition...Debug...">
</QUOTE>

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Sanjib Biswas said:
Unfortunately, I don't see the Advance Compiler Settings. I checked the
help page also
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vsprojopt/html/1f81133a-293f-4dba-bc1c-8baafb01d857.htm.
I am not sure what's causing VB Express edition to not to show this
button. I am using V8.0.50727.42.

Regards
Sanjib
 
Thanks a lot Greg. But my problem remains the same as before. I can see in
the compilation, Vbc is adding /removeintchecks+ to remove overflow check
but when I run the application, I get the exception in the same line.

Regards
Sanjib

Greg Young said:
Ah express .. See
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=141019&SiteID=1

I will c/p here for aggregators.

<QUOTE>
Then add the following line

<RemoveIntegerChecks>true</RemoveIntegerChecks>

in the containers

<PropertyGroup Condition...Release...">
<PropertyGroup Condition...Debug...">
</QUOTE>

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 
Back
Top