Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Bytes overflowing despite modulo... [Solved]

Bytes overflowing despite modulo... [Solved]

Scheduled Pinned Locked Moved Visual Basic
helpcsharp
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saul Johnson
    wrote on last edited by
    #1

    Hello, I've been thinking about this one for a long time. The following code always gives me an OverflowException when I run it:

    Dim x As Byte = 200
    Dim y As Byte = 100
    Dim z As Byte = (x + y) Mod 256

    I know the problem is the fact that I'm adding two byte values together, which is exceeding the limits of the data type in the intermediate step (x + y) before the modulo 256 is performed and the result assigned to z. I can fix it if I do this:

    Dim x As Byte = 200
    Dim y As Byte = 100
    Dim z As Byte = (CInt(x) + CInt(y)) Mod 256

    But that just seems like a lot of bother to achieve something that should be simple. One of my latest projects involved a lot of arithmetic like this and I can't help but wonder whether or not there's a better way of performing pure byte arithmetic with modulo operations without resorting to declaring larger integers all over the place or casting to and from them in code. SixOfTheClock

    A programming language is to a programmer what a fine hat is to one who is fond of fancy garden parties. Just don't try wearing any .NET language on your head. Some of them are sharp.

    D 1 Reply Last reply
    0
    • S Saul Johnson

      Hello, I've been thinking about this one for a long time. The following code always gives me an OverflowException when I run it:

      Dim x As Byte = 200
      Dim y As Byte = 100
      Dim z As Byte = (x + y) Mod 256

      I know the problem is the fact that I'm adding two byte values together, which is exceeding the limits of the data type in the intermediate step (x + y) before the modulo 256 is performed and the result assigned to z. I can fix it if I do this:

      Dim x As Byte = 200
      Dim y As Byte = 100
      Dim z As Byte = (CInt(x) + CInt(y)) Mod 256

      But that just seems like a lot of bother to achieve something that should be simple. One of my latest projects involved a lot of arithmetic like this and I can't help but wonder whether or not there's a better way of performing pure byte arithmetic with modulo operations without resorting to declaring larger integers all over the place or casting to and from them in code. SixOfTheClock

      A programming language is to a programmer what a fine hat is to one who is fond of fancy garden parties. Just don't try wearing any .NET language on your head. Some of them are sharp.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Nope. That's the simplest and fastest way to do it. Well, it'll be a tiny bit faster if you just used integers to begin with.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Nope. That's the simplest and fastest way to do it. Well, it'll be a tiny bit faster if you just used integers to begin with.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        S Offline
        S Offline
        Saul Johnson
        wrote on last edited by
        #3

        Well, couldn't have asked for it any clearer than that! Thank you. :)

        A programming language is to a programmer what a fine hat is to one who is fond of fancy garden parties. Just don't try wearing any .NET language on your head. Some of them are sharp.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups