Search code examples
c#bytecarryflag

What actually happens when a Byte overflows?


What actually happens when a Byte overflows?

Say we have

byte byte1 = 150; // 10010110  
byte byte2 = 199; // 11000111

If we now do this addition

byte byte3 = byte1 + byte2;

I think we'll end up with byte3 = 94 but what actually happens? Did I overwrite some other memory somehow or is this totally harmless?


Solution

  • It's quite simple. It just does the addition and comes off at a number with more than 8 bits. The ninth bit (being one) just 'falls off' and you are left with the remaining 8 bits which form the number 94.

    (yes it's harmless)