Step 1: Represent the numbers in binary form:
Let’s take two binary numbers A and B:
A = 1011 (which represents -5 in decimal using two’s complement) B = 1101 (which represents -3 in decimal using two’s complement)
Step 2: Determine the sign bits:
In two’s complement representation, the leftmost bit (most significant bit) represents the sign. If it’s 0, the number is positive, and if it’s 1, the number is negative.
For A: The leftmost bit is 1, so A is negative. For B: The leftmost bit is 1, so B is negative.
Step 3: Perform addition ignoring overflow:
Start from the rightmost bit (least significant bit) and move towards the leftmost bit.
Add each pair of bits along with the carry generated from the previous addition.
Step 4: Check for overflow:
Overflow happens when the result exceeds the range representable by the number of bits. In two’s complement representation, if the sum of two negative numbers results in a positive number, or if the sum of two positive numbers results in a negative number, overflow has occurred.
Step 5: Adjust the result if overflow occurs:
If overflow happens, you need to adjust the result to make it valid.
Step 6: Final result:
If there’s no overflow, the result obtained is the correct sum.
In our example: A + B = 10110 (which represents -8 in decimal using two’s complement)
So, the sum of -5 and -3 in two’s complement representation is -8.
In this table:
- Step 1: We start adding the bits from the rightmost side (least significant bit) towards the leftmost side.
- Step 2: We add each pair of bits along with the carry from the previous addition.
- Step 3: We continue the addition until all bits are processed.
- Step 4: We check for overflow, and if overflow occurs, we discard the carry.
- Finally, we have the result, which is 10110 in binary, representing -8 in decimal using two’s complement representation.