3

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. ;-) Commented Mar 23, 2018 at 18:23

2 Answers 2

1

The entire solution is very misleading, and wrong IMHO.

unsigned 214 = 11010110
unsigned 151 = 10010111

As signed 8 bit numbers in two-complements notation, you get

11010110 = signed -42  (42  = 00101010 = ones complement + 1)
10010111 = signed -105 (105 = 01101001 = ones complement + 1)

You will notice that

214 + 42 = 256 = (11111111) + 1
151 + 105 = 256

That is why the two-complements notation works for addition (modulo 256): -1 + 1 = 0 etcetera. The conversion: ones complement + 1, was okay, though at one spot numbers were swapped.

For addition overflow cannot happen when both numbers have different signs.

11010110
10010111
-------- +
01101101
^ overflow

So overflow of x+y when x7 = y7 and x7sum7.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

thanks! one question, when you said "So to tell which number 11010110 represents, take the two's complement, then negate that.", what do you mean by negating that?
When you complement it, you get 00101010. That's 42. So 11010110 is -42.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.