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 AsByte = 200Dim y AsByte = 100Dim z AsByte = (x + y) Mod256
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 AsByte = 200Dim y AsByte = 100Dim z AsByte = (CInt(x) + CInt(y)) Mod256
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.
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.