Bytes overflowing despite modulo... [Solved]
-
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 256I 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 256But 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.
-
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 256I 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 256But 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.
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 -
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 KreskowiakWell, 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.