1

Perform the following addition of $2$’s complement numbers. Indicate whether or not the sum overflows a $4$-bit result: $1101 + 0100$

If I add, the answer is $10001$. It is five bits, so overflows. If I convert the numbers to decimal and add, it is $13 + 4 = 17$.

However, the notes say the answer is $0001$, no overflow. And that converting results in $3 + 4 = 1$.

How can that be? Which is right?

  • Clearly 1101 is not 13.

    Keep in mind you have to follow twos complement arithmetic rules, not simple binary.

    – AgentS Feb 19 '18 at 02:57
  • @rsadhvika, But then the alternative is -5 (1 msb meaning negative, 101 = 5). Yet the notes say it's supposed to be -3. – user532825 Feb 19 '18 at 03:00
  • 1101 is -5 in signed magnitude. Its not -5 in twos complement. Do you know how to take twos complement of a number ? – AgentS Feb 19 '18 at 03:01
  • @rsadhvika - It is negative because of the msb, so invert to 0010 + 1 = 0011 = +3. But the notes say negative 3. – user532825 Feb 19 '18 at 03:04
  • In twos complement representation also, msb tells you whether a number is negative or positive – AgentS Feb 19 '18 at 03:06
  • @rsadhvika - is that before or after the inversion? Before it is 1101, so it's negative. But after it is 0011, so positive. – user532825 Feb 19 '18 at 03:13
  • Before the inversion. Look at it this way : 1101 is stored in a computer, You can interpret the binary number 1101 in any way you want based on the arithmetic system you choose. In twos complement, the msb 1 tells you to treat the stored number as negative. To know what "magnitude" 1101 represents, you have to take the twos complement of 1101. – AgentS Feb 19 '18 at 04:02
  • @rsadhvika - Thanks, I think I see. But what about the example of "0111 + 1000"? It is positive 7 minus 8 = -1 . But adding makes 1111 = 15. I could take the two's complement of it for 0001. But which is the final answer - 1111 or 0001 ? – user532825 Feb 19 '18 at 04:31
  • Your answer is just 1111. This represents -1 in twos complement notation. msb 1 tells you 1111 is negative. To know what "magnitude" 1111 represents, you take the twos complement of 1111. – AgentS Feb 19 '18 at 05:46
  • As you can see, this circus needs to be done only for negative numbers. For a positive number like 0110, the binary and twos complement represent the same thing. 0110 = +2 in binary system, it is also +2 in twos complement system. – AgentS Feb 19 '18 at 05:50
  • For 1111, it represents 15 in binary system and -1 in twos complement system. Computer doesn't care what system you use. It simply stores the binary 1111. You can interpret it as binary 15, or signed magnitude -7, or twos complement -1. Upto you. – AgentS Feb 19 '18 at 05:53
  • Ah and do not put - or + before a number in twos complement notation. MSB tells you whether the number is positive or negative. The binary string "-1111" doesn't make much sense. It can be helpful if you consider how the computer stores numbers. Twos complement system is for storing numbers and operating on them in computers. Computer knows only 0s and 1s. It doesn't know -. You either say the answer is 1111 in twos complement or it is -1 in decimal. – AgentS Feb 19 '18 at 06:00
  • @rsadhvika I see now. Thanks for your help. – user532825 Feb 20 '18 at 02:19

0 Answers0