July 28th, 2013, 09:45 PM
Please learn to use code blocks
to maintain proper code formatting.
In C#, when you add two bytes together the result is always an int. This is because you can't accurately store every result of a byte + byte operation in a byte due to potential overflow. Because C# will only implicitly convert to a larger size, you'll have to explicitly convert the result with a cast. You have three options:
c = checked( (byte)(a + b) ) ;
c = (byte)(a + b) ;
c = (byte)( (a + b) % 255 ) ;
The second unchecked variation is inherently unsafe and may not yield the results you expect. The third option also loses information and may or may not be equivalent the second.
I no longer wish to be associated with this site.
Last edited by jwdonahue : July 28th, 2013 at 09:46 PM.