Search code examples
binarysubtractionadditiontwos-complement

Need some assistance understanding binary addition/subtraction using 2's complement


If A = 01110011, B = 10010100, how would I add these?

I did this: enter image description here

i.e: 01110011 + 10010100 = 100000111

Though, isn't it essentially 115 + (-108) = 7, whereas, I'm getting -249

Edit: I see that removing the highest order bit (overflow) I get 7 which is what I'm looking for but I'm not getting why you wouldn't have the extra bit.

Edit**: Ok, I figured it out. There was no overflow as I had assumed there was because 7 is within [-128, 127] (8-bits). Instead, like Omar hinted at I was supposed to drop the "extra" 1 from addition.


Solution

  • Your calculation is correct and the result is correct.

    You stated that the second number is -108, so both your numbers are interpreted as signed 8-bit values. Thus, you should also interpret your result as an 8-bit signed value, this is why the 9th bit must be dropped, and so the result is 7 (00000111).

    On a real hardware, like an 8-bit CPU for example, as all the registers are 8-bit wide, you are only be able to store the lowest 8-bit of the result, which here is 7 (00000111). In some cases, the 9th bit may also be put inside a carry/overflow flag so it's not completely "dropped".