I did the following exercise but even though I've revised my calculations several times, I don't seem to come up with the right answer:

Given a 8-bit processor and the variables $a=30$ and $b=70$, calculate $b-a$ in 2's complement. Indicate if there is a carry-out and overflow.

a. -40, there's overflow and carry-out

b. 40, there isn't overflow but there is carry-out

c. 40, there isn't overflow or carry-out

d. 296, there is overflow and carry-out

Apparently the right answer is b but I don't arrive to the same answer. This is what I do:

I first convert the previous numbers to the binary system.

$$70_{_{10}}=1000110_{2}$$ $$30_{_{10}}=11110_{2}$$

Since, we're dealing with 2's complements, I need to add a 0 at the beggining of each binary number. There:

$$70_{_{10}}=01000110_{2}$$ $$30_{_{10}}=011110_{2}$$

Since, the exercise wants me to subtract, I need to convert 30 to -30. To do that, I flip all the digits of 30 and add 1 up:

$$30_{10}=011110_{2}\Rightarrow 100001_{2} $$ $$100001_{2} + 1_{2} = 100010_{2}$$ Then, $$-30_{10} = 100010_{2}$$ Therefore, I just need to sum them: enter image description here

(Since there are more bits in 70 than in -30, I have to add 1s at the beggining of the binary number that represents -30)

Hence, I've found overflow and carry-out and it gives me $100101000_{2}$, which is -216.

Does anyone see the problem?

Thank you in advance.

  • $\begingroup$ Unless 70-30 gives you 40, there is a problem with your calculations. You need to fill out the binary representation of 30 to 8 bits before you take the 2-s complement. $\endgroup$
    – saulspatz
    Mar 28 '18 at 21:38
  • 1
    $\begingroup$ What are the definitions of "overflow" and "carry out" for your processor? These are not standard terms with a fixed mathematical meaning. Moreover, in subtraction, you don't "carry" but rather you "borrow" and you need to say how that is represented in the processor of interest. $\endgroup$
    – Rob Arthan
    Mar 28 '18 at 21:51
  • $\begingroup$ Bit 8 is the sign bit. Carry-out into bit 9 is discarded. $\endgroup$
    – dan post
    Mar 28 '18 at 22:09
  • $\begingroup$ @RobArthan As far as I know, when you're summing them, if you sum 1+1, it means that you carry out a 1 and I'd the result has more bits than the processor, overflow occurs $\endgroup$
    – Arnau
    Mar 28 '18 at 22:42
  • $\begingroup$ "As far as I know" isn't good enough. You need to work from a precise definition. $\endgroup$
    – Rob Arthan
    Mar 28 '18 at 22:57

$$\underbrace{/\!\!\!1}_{\text{carry out}}\underbrace{0010}_{2}\;\underbrace{1000_2}_{8} = 28_{16} = 40_{10}$$ there is a carry out, but there is no overflow bit.

Edit The carry-out refers to the bit that is "carried out" from the 8 bit adder, i.e., the 9th bit above. This is typically may used in conjunction with another adder (as the carry in) to make longer adders. It tells you nothing about whether or not the signed arithmetic being performed was successful.

The overflow checks the most significant bit of the 8 bit result. This is the sign bit. If we add two negative numbers (MSBs$=1$) then the result should be negative (MSB$=1$), whereas if we add two positive numbers (MSBs$=0$) then the result should be positive (MSBs$=0$), so the MSB of the result must be consistent with the MSBs of the summands if the operation was successful, otherwise the overflow bit is set.

  • $\begingroup$ But if you have a 9th bit, it means that you have overflow right? $\endgroup$
    – Arnau
    Mar 29 '18 at 7:59
  • $\begingroup$ No that is the carry out. I recommend reading: teaching.idallen.com/dat2343/10f/notes/040_overflow.txt Specific to this: "A human need only remember that, when doing signed math, adding two numbers of the same sign must produce a result of the same sign,otherwise overflow happened." $\endgroup$
    – adfriedman
    Mar 29 '18 at 19:25
  • $\begingroup$ Also: "The rules for two's complement detect errors by examining the sign of the result. A negative and positive added together cannot be wrong, because the sum is between the addends. Since both of the addends fit within the allowable range of numbers, and their sum is between them, it must fit as well. Mixed-sign addition never turns on the overflow flag." $\endgroup$
    – adfriedman
    Mar 29 '18 at 19:28
  • $\begingroup$ Okay, I understand it now. I think the problem was that I didn't quite get the meaning of 'carry-out' and 'overflow'. $\endgroup$
    – Arnau
    Mar 30 '18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.