473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ulong vs UInt64

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[i];
}

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.Incre ment(i), i - 1)
End While

Return hash

End Function
Jul 4 '06 #1
9 4140
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]
- mv*@spam.guard.caspershouse.com

"Sanjib Biswas" <sa***********@ieee.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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[i];
}

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.Incre ment(i), i - 1)
End While

Return hash

End Function


Jul 4 '06 #2
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" <sa***********@ieee.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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[i];
}

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.Incre ment(i), i - 1)
End While

Return hash

End Function


Jul 4 '06 #3
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
<Exceptionin 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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:O4**************@TK2MSFTNGP04.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Sanjib Biswas" <sa***********@ieee.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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[i];
Console.WriteLine("Intermediate Hash : {0}, {1}", hash, (hash <<
17));
Console.WriteLine("data[{0}] : {1}", i, (0 + data[i]));
> }
Console.WriteLine("Returning Hash : {0}", hash);
> 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.Incre ment(i), i - 1)
End While

Return hash

End Function



Jul 5 '06 #4
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" <dr*******************@hotmail.comwrote in message
news:e9**************@TK2MSFTNGP04.phx.gbl...
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" <sa***********@ieee.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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[i];
}

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.Incre ment(i), i - 1)
End While

Return hash

End Function



Jul 5 '06 #5
Sanjib Biswas <sa***********@ieee.orgwrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 5 '06 #6
Glad I refreshed! I was just about to post this :)

Thanks Jon!

Cheers,

Greg
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Sanjib Biswas <sa***********@ieee.orgwrote:
> 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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 5 '06 #7
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

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Sanjib Biswas <sa***********@ieee.orgwrote:
> 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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 5 '06 #8
Ah express .. See
http://forums.microsoft.com/MSDN/Sho...41019&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" <sa***********@ieee.orgwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
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

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>Sanjib Biswas <sa***********@ieee.orgwrote:
>> 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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jul 5 '06 #9
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" <dr*******************@hotmail.comwrote in message
news:ex**************@TK2MSFTNGP02.phx.gbl...
Ah express .. See
http://forums.microsoft.com/MSDN/Sho...41019&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" <sa***********@ieee.orgwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
>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

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft. com...
>>Sanjib Biswas <sa***********@ieee.orgwrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Jul 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt Creely | last post by:
I'm creating a DTS package to import some data from our HP3000, using the minisoft odbc driver as a linked server within sql server. I'm importing a table from the HP3000 (which I've done lots of...
5
by: ThunderMusic | last post by:
Hi, I have a problem, I have to do this (MyUInt64 / MyOtherUInt64). the IDE tells me that the / operator has not been overloaded for UInt64. Is there a way I can do what I need to do? Thanks
3
by: ThunderMusic | last post by:
Hi, I have 2 UInt64 to add and then divide the result by another value. How can I do this? because the math operators have not been defined for UInt64. Can somebody help please? Thanks ...
2
by: donbro | last post by:
If my read of the extension source (Mac/Modules/file/_Filemodule.c) is correct, the parameter sizes specified for data and resource file sizes are UInt32 where they should be UInt64. In both OS9...
2
by: Nadav | last post by:
How can a UInt64 be converted to a byte ????
12
by: Wolfgang Kaml | last post by:
Dear all, I am using the following code to retrieve the size of a certain file and the available (free) space on the disk. The problem is, that I get the size of the file returned as a Long and...
1
by: John Smith | last post by:
Hello Whenever I try to set a value of a System.UInt64 I get: Dim var1 As System.UInt64 = &H18B377544DD23334 Error BC30311: Value of type 'Long' cannot be converted to 'System.UInt64'. ...
8
by: Kevin | last post by:
Dim X as int32 Dim Y as int64 X = Y / 1024 gives me the error: "Operator is not valid for type 'UInt64' and type 'Integer'." I'm new to VB.NET and this is one of the reasons why I've...
9
by: Sanjib Biswas | last post by:
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'...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.