ulong vs UInt64

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sanjib Biswas

    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.Len gth;

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

    return hash;
    }

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

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

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

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

    Return hash

    End Function


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: ulong vs UInt64

    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]
    - mvp@spam.guard. caspershouse.co m

    "Sanjib Biswas" <sanjib.biswas@ ieee.orgwrote in message
    news:%23Naudj4n GHA.4172@TK2MSF TNGP03.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.Len gth;
    >
    for (int i = 0; i < data.Length; i++)
    {
    hash += (hash << 17) + data[i];
    }
    >
    return hash;
    }
    >
    VB.Net Code:
    ==========
    >
    Private Shared Function GetUInt64Hash(B yVal data As String, ByVal hash As
    System.UInt64) As System.UInt64
    >
    hash += (hash << 13) + CType(data.Leng th, System.UInt64)
    Dim i As Integer = 0
    >
    While i < data.Length
    hash += (hash << 17) + Asc(data(i))
    System.Math.Min (System.Threadi ng.Interlocked. Increment(i), i - 1)
    End While
    >
    Return hash
    >
    End Function
    >
    >

    Comment

    • Greg Young

      #3
      Re: ulong vs UInt64

      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#


      "Sanjib Biswas" <sanjib.biswas@ ieee.orgwrote in message
      news:%23Naudj4n GHA.4172@TK2MSF TNGP03.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.Len gth;
      >
      for (int i = 0; i < data.Length; i++)
      {
      hash += (hash << 17) + data[i];
      }
      >
      return hash;
      }
      >
      VB.Net Code:
      ==========
      >
      Private Shared Function GetUInt64Hash(B yVal data As String, ByVal hash As
      System.UInt64) As System.UInt64
      >
      hash += (hash << 13) + CType(data.Leng th, System.UInt64)
      Dim i As Integer = 0
      >
      While i < data.Length
      hash += (hash << 17) + Asc(data(i))
      System.Math.Min (System.Threadi ng.Interlocked. Increment(i), i - 1)
      End While
      >
      Return hash
      >
      End Function
      >
      >

      Comment

      • Sanjib Biswas

        #4
        Re: ulong vs UInt64

        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, 810668720751902 72
        data[1] : 55
        Intermediate Hash : 810674905663407 48, 353537054712791 040
        data[2] : 0
        Intermediate Hash : 434604545279131 908, 941259211282055 168
        data[3] : 120
        Intermediate Hash : 137586375656118 7124, 184423540334211 8912
        data[4] : 48
        Intermediate Hash : 322009915990330 6085, 333268037159419 9040
        data[5] : 49
        Intermediate Hash : 655277953149750 5243, 551468052428396 9536
        data[6] : 118
        Intermediate Hash : 120674600557814 74880, 850057523768170 9056
        data[7] : 101
        <Exceptionin the 8th iteration

        C# Code:
        ========
        Length : 36
        First Hash : 36
        Intermediate Hash : 4718677, 618486431744
        data[0] : 49
        Intermediate Hash : 618491150476, 810668720751902 72
        data[1] : 55
        Intermediate Hash : 810674905663407 48, 353537054712791 040
        data[2] : 0
        Intermediate Hash : 434604545279131 908, 941259211282055 168
        data[3] : 120
        Intermediate Hash : 137586375656118 7124, 184423540334211 8912
        data[4] : 48
        Intermediate Hash : 322009915990330 6085, 333268037159419 9040
        data[5] : 49
        Intermediate Hash : 655277953149750 5243, 551468052428396 9536
        data[6] : 118
        Intermediate Hash : 120674600557814 74880, 850057523768170 9056
        data[7] : 101
        Intermediate Hash : 212129121975363 2434, 125560765977484 32896
        data[8] : 114
        Intermediate Hash : 146773678175020 65445, 179086166090030 77632
        data[9] : 115
        Intermediate Hash : 141392403527955 91566, 636815639367463 7312
        data[10] : 105
        Intermediate Hash : 206065267276067 7373, 150871409059594 24000
        data[11] : 111
        Intermediate Hash : 171477935787201 01483, 740852108195358 3104
        data[12] : 110
        Intermediate Hash : 610957058696413 3032, 202899075749956 8128
        data[13] : 61
        Intermediate Hash : 813856134446370 1194, 176429911440016 01536
        data[14] : 34
        Intermediate Hash : 733480841475575 1163, 174943934188244 17280
        data[15] : 49
        Intermediate Hash : 638245775987061 6873, 165975903332899 2256
        data[16] : 46
        Intermediate Hash : 804221679319960 9177, 714291427426605 4656
        data[17] : 48
        Intermediate Hash : 151851310674656 63867, 156006978937132 15488
        data[18] : 34
        Intermediate Hash : 123390848874693 27771, 869445196850121 9328
        data[19] : 32
        Intermediate Hash : 258679278226099 5584, 494748173165448 3968
        data[20] : 101
        Intermediate Hash : 753427451391547 9662, 443184596261404 6720
        data[21] : 110
        Intermediate Hash : 119661204765295 26481, 737497659717831 8848
        data[22] : 99
        Intermediate Hash : 894352999998293 824, 140245714258771 31264
        data[23] : 111
        Intermediate Hash : 149189244258754 25188, 615681476271118 7456
        data[24] : 100
        Intermediate Hash : 262899511487706 1133, 246840027173263 7696
        data[25] : 105
        Intermediate Hash : 509739538660969 8939, 318450802020935 2704
        data[26] : 110
        Intermediate Hash : 828190340681905 1746, 854157707447605 6576
        data[27] : 103
        Intermediate Hash : 168234804812951 08383, 340561220064903 168
        data[28] : 61
        Intermediate Hash : 171640417013600 11585, 157068832636520 36608
        data[29] : 34
        Intermediate Hash : 144241808913024 96694, 178844143826111 03744
        data[30] : 117
        Intermediate Hash : 138618512002040 48938, 694971719652553 5232
        data[31] : 116
        Intermediate Hash : 236482432302003 2656, 161299634012448 3584
        data[32] : 102
        Intermediate Hash : 397782066314451 6285, 213546035127163 2896
        data[33] : 45
        Intermediate Hash : 611328101441614 9237, 874679183171924 7872
        data[34] : 56
        Intermediate Hash : 148600728461353 97143, 310157788834784 8704
        data[35] : 34
        Returning Hash : 148600728461353 97143

        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
        message news:O4D2Rp4nGH A.3784@TK2MSFTN GP04.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]
        - mvp@spam.guard. caspershouse.co m
        >
        "Sanjib Biswas" <sanjib.biswas@ ieee.orgwrote in message
        news:%23Naudj4n GHA.4172@TK2MSF TNGP03.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.WriteLi ne("Length : {0}", data.Length);
        > hash += ( hash << 13 ) + (ulong)data.Len gth;
        Console.WriteLi ne("First Hash : {0}", hash);
        >>
        > for (int i = 0; i < data.Length; i++)
        > {
        > hash += (hash << 17) + data[i];
        Console.WriteLi ne("Intermediat e Hash : {0}, {1}", hash, (hash <<
        17));
        Console.WriteLi ne("data[{0}] : {1}", i, (0 + data[i]));
        > }
        Console.WriteLi ne("Returning Hash : {0}", hash);
        > return hash;
        >}
        >>
        >VB.Net Code:
        >==========
        >>
        >Private Shared Function GetUInt64Hash(B yVal data As String, ByVal hash As
        >System.UInt6 4) As System.UInt64
        >>
        > hash += (hash << 13) + CType(data.Leng th, System.UInt64)
        > Dim i As Integer = 0
        >>
        > While i < data.Length
        > hash += (hash << 17) + Asc(data(i))
        > System.Math.Min (System.Threadi ng.Interlocked. Increment(i), i - 1)
        > End While
        >>
        > Return hash
        >>
        >End Function
        >>
        >>
        >
        >

        Comment

        • Sanjib Biswas

          #5
          Re: ulong vs UInt64

          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" <druckdruckREMO VEgoose@hotmail .comwrote in message
          news:e9bWit4nGH A.3320@TK2MSFTN GP04.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#

          >
          "Sanjib Biswas" <sanjib.biswas@ ieee.orgwrote in message
          news:%23Naudj4n GHA.4172@TK2MSF TNGP03.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.Len gth;
          >>
          > for (int i = 0; i < data.Length; i++)
          > {
          > hash += (hash << 17) + data[i];
          > }
          >>
          > return hash;
          >}
          >>
          >VB.Net Code:
          >==========
          >>
          >Private Shared Function GetUInt64Hash(B yVal data As String, ByVal hash As
          >System.UInt6 4) As System.UInt64
          >>
          > hash += (hash << 13) + CType(data.Leng th, System.UInt64)
          > Dim i As Integer = 0
          >>
          > While i < data.Length
          > hash += (hash << 17) + Asc(data(i))
          > System.Math.Min (System.Threadi ng.Interlocked. Increment(i), i - 1)
          > End While
          >>
          > Return hash
          >>
          >End Function
          >>
          >>
          >
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: ulong vs UInt64

            Sanjib Biswas <sanjib.biswas@ 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 - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Greg Young

              #7
              Re: ulong vs UInt64

              Glad I refreshed! I was just about to post this :)

              Thanks Jon!

              Cheers,

              Greg
              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:MPG.1f151c 8c54a34a7998d2b e@msnews.micros oft.com...
              Sanjib Biswas <sanjib.biswas@ 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 - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Sanjib Biswas

                #8
                Re: ulong vs UInt64

                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.ht m.
                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]" <skeet@pobox.co mwrote in message
                news:MPG.1f151c 8c54a34a7998d2b e@msnews.micros oft.com...
                Sanjib Biswas <sanjib.biswas@ 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 - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Greg Young

                  #9
                  Re: ulong vs UInt64

                  Ah express .. See


                  I will c/p here for aggregators.

                  <QUOTE>
                  Then add the following line

                  <RemoveIntegerC hecks>true</RemoveIntegerCh ecks>

                  in the containers

                  <PropertyGrou p Condition...Rel ease...">
                  <PropertyGrou p Condition...Deb ug...">
                  </QUOTE>

                  Cheers,

                  Greg Young
                  MVP - C#


                  "Sanjib Biswas" <sanjib.biswas@ ieee.orgwrote in message
                  news:OLjOKQ9nGH A.5092@TK2MSFTN GP03.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.ht m.
                  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]" <skeet@pobox.co mwrote in message
                  news:MPG.1f151c 8c54a34a7998d2b e@msnews.micros oft.com...
                  >Sanjib Biswas <sanjib.biswas@ 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 - <skeet@pobox.co m>
                  >http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  >If replying to the group, please do not mail me too
                  >
                  >

                  Comment

                  • Sanjib Biswas

                    #10
                    Re: ulong vs UInt64

                    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" <druckdruckREMO VEgoose@hotmail .comwrote in message
                    news:exmn9S9nGH A.2100@TK2MSFTN GP02.phx.gbl...
                    Ah express .. See

                    >
                    I will c/p here for aggregators.
                    >
                    <QUOTE>
                    Then add the following line
                    >
                    <RemoveIntegerC hecks>true</RemoveIntegerCh ecks>
                    >
                    in the containers
                    >
                    <PropertyGrou p Condition...Rel ease...">
                    <PropertyGrou p Condition...Deb ug...">
                    </QUOTE>
                    >
                    Cheers,
                    >
                    Greg Young
                    MVP - C#

                    >
                    "Sanjib Biswas" <sanjib.biswas@ ieee.orgwrote in message
                    news:OLjOKQ9nGH A.5092@TK2MSFTN GP03.phx.gbl...
                    >Unfortunatel y, 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.ht m.
                    >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]" <skeet@pobox.co mwrote in message
                    >news:MPG.1f151 c8c54a34a7998d2 be@msnews.micro soft.com...
                    >>Sanjib Biswas <sanjib.biswas@ ieee.orgwrote:
                    >>> How do I turn off overflow check in VB.Net? I couldn't find this in
                    >>>the
                    >>>applicatio n 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 - <skeet@pobox.co m>
                    >>http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    >>If replying to the group, please do not mail me too
                    >>
                    >>
                    >
                    >

                    Comment

                    Working...