dcsimg
Results 1 to 6 of 6
  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2015
    Posts
    53

    Resolved [RESOLVED] Adding number to variable and allowing wrapping, rather than overflow

    Hi, I primarily work in embedded systems, but dabble in .NET.

    In C, if I have a byte, and I keep adding 1, once I get to 255, it will wrap to 0. In .NET, it overflows. I need it to wrap. Does anyone know how to make this happen in the background without making a function? Thx,

    D

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2015
    Posts
    53

    Re: Adding number to variable and allowing wrapping, rather than overflow

    That does not work. I am actually using 32 bit numbers.

    For i As Integer = 0 To byteRead.Length / 4 Step 1
    crc_check = ((crc_check + integerStore(i)) Mod 2 ^ 32)
    Next

    integerStore is data from a bin file, so it can be anything. crc_check is the crc of the bin file. Adding them to the original crc should result in a sum of zero (just a little background)

    I am still getting overflow results.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,447

    Re: Adding number to variable and allowing wrapping, rather than overflow

    Do you want to use UInt32 instead of Integer?
    Or perhaps if you want to use Mod, then use a Long instead of an Integer for the values during your calculations.

    Also, masking the lower 32 bits is actually a bit faster than using Mod.
    A quick example test.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
     
       Dim crc_check As Long
        Dim byteRead() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
        Dim integerStore() As Integer = {&H4030201, &H80070605, &HC00B0A09, &H100F0E0D}
    
        For i As Integer = 0 To CInt(byteRead.Length / 4) - 1 Step 1
          crc_check = ((crc_check + integerStore(i)) And &HFFFFFFFF&)
        Next
        Debug.Print(crc_check.ToString)
      End Sub
    p.s. The second and third values in integerStore() were chosen to cause an overflow with your original code. Changing crc_check to a Long type prevents the overflow.
    Last edited by passel; Jun 12th, 2018 at 01:47 PM.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,131

    Re: Adding number to variable and allowing wrapping, rather than overflow

    I haven't tested this fully, but I believe you can do this for the entire project by going to the Compile tab, then the Advanced Compile Options, and checking the box for Remove integer overflow checks.

    According to the documentation, this will ignore any high bits resulting from an overflow. The following did as expected:
    Code:
     Dim usign As UInt32 = UInteger.MaxValue
    
            MessageBox.Show(usign.ToString)
            usign += 1
            MessageBox.Show(usign.ToString)
            usign += 4
            MessageBox.Show(usign.ToString)
    The messages showed the max value, then 0, then 4.

    That's a wrap.

    EDIT: By the way, this can only be set for the whole project in VB.NET. I believe it can be set at a fine granularity in C#, so if you don't want to remove integer overflow checks for the whole project, you could move the key methods into a C# dll, then reference that in the VB project.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2015
    Posts
    53

    Re: Adding number to variable and allowing wrapping, rather than overflow

    The last response worked great, thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.