Daniel B. answered 01/31/23
PhD in Computer Science with 42 years in Computer Research
This question cannot be answered without knowing the word length.
I will assume that it is 8 bits.
If my assumption is wrong then hopefully you will still be able to extend the explanation
to the correct word length.
When using 2's compliment, subtraction is converted into addition.
There are two ways of thinking about it.
Method 1:
Given that the word length is 8, any pair of numbers that differ by exactly 256
have the same binary representation.
(It is 256 because 256 = 28.)
So 42 - 18 has the same binary representation
as 42 - 18 + 256 = 42 + 238
Thus the binary representation of 42 - 18 is the same as
the binary representation of 42 + 238.
So we need to do binary addition of these two
00101010
11101110
Method 2:
This is a more mechanical approach which is actually implemented in hardware.
The binary representation of 18 is
00010010
The binary representation of -18 is obtained from the bitwise complement
11101101
and adding +1, resulting in
11101110
Notice that the representation of -18 is the same as the representation of 238.
And the reason is that they differ by 256.
Using either method we end up with the addition
00101010
11101110
--------------
00011000
which is the binary representation of 24.
Notice that the binary addition resulted in a carry out of the leading bit.
That does not represent an overflow -- in case of subtraction the carry is
the normal thing to happen.