r/AskComputerScience • u/scarcelyberries • Aug 26 '25
Adding with 2s complement
I'm learning to add with 2s complement, and I think I have it down for the most part but there's something still tripping me up. I'm using 6-bits.
When I add -25 and -8, I get
1011111 which seems correct as is and has a leading 1 to show negative, but overflows my 6 bits
When I add +25 and -8, I get
011001 for 25 and for 8 I have 001000, flip and add 1 for 110111 into 111000
Then when I add 011001 and 111000 I get 1010001 instead of the expected 010001. Why does the overflow on this one make it a different number? Why does it not lead with a zero? Am I missing something? I feel like I'm skipping something important but don't know what
Please help, I've been looking at this and googling for an hour and haven't found an explanation
1
u/zmerlynn Aug 26 '25
The carried out bit in two’s complement is simply discarded, so your 1010001 is 010001, which is correct. (Overflow detection is done by checking signs vs the carry bit.)
1
u/stevevdvkpe Aug 26 '25
A 6-bit twos-complement number can represent values between -32 (100000) and 31 (011111).
-25 (100111) + -8 (111000) overflows, because the result -33 can't be represented in 6-bit twos-complement. Hardware detects overflow by noting whether there was a carry in to the most significant bit.
25 (011001) + 8 (001000) also overflows because 33 can't be represented in 6-bit twos-complement.