Click here to Skip to main content
       

Visual Basic

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionBytes overflowing despite modulo... [Solved] [modified]memberSixOfTheClock16 Sep '12 - 13:28 
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.


modified 17 Sep '12 - 5:21.

AnswerRe: Bytes overflowing despite modulo...mvpDave Kreskowiak16 Sep '12 - 17:46 
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.

GeneralRe: Bytes overflowing despite modulo...memberSixOfTheClock16 Sep '12 - 23:22 
Well, couldn't have asked for it any clearer than that! Thank you. Smile | :)
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.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 21 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid