1
votes

Given Question:

Assume 151 and 214 are signed 8-bit decimal integers stored in two’s complement format. Calculate 151+214, the result should be written in decimal. Check for overflow. Hint: overflow occurs when the result of adding two positive numbers is negative.

Instructor's Solution:

Step 1:

Write the two numbers in binary:

214 = 11010110

151 = 10010111

Step 2:

Rewrite using two's complement representation:

214 = 11010110 = 00101010

151 = 10010111 = 01101001

Step 3:

Convert results of two’s complement to decimal:

214 = 11010110 = 00101010 = -105

151 = 10010111 = 01101001 = -42

Step 4:

Add: 100101010+01101001 = 10010011

Step 5:

Convert results to decimal: 10010011 = -147

Conclusion: As can be noted, there is overflow

My questions are:

In step 1: Why are the MSBs of the results of converting 214 and 151 to binary equal to 1 (negative) when 214 and 151 are both positive? Aren’t their values in binary equal to 011010110 and 010010111 respectively?

In step 3: After converting the results of two’s complement of both numbers to decimal, why is the sign negative if the MSB is 0 (positive)?

1
The MSB is 1 because the values exceed the signed range for 8-bit values (0..127). And a computer can't just add another bit. ;-)DevSolar

1 Answers

0
votes

In step 1, the numbers are too large for an 8 bit two's complement number. The largest 8 bit signed integer is 127. Step 3 is showing that if you write those numbers in 8 bits, you get a negative number. The conversion is showing which negative number you get. So to tell which number 11010110 represents, take the two's complement, then negate that.