Binary arithmetic - overflow and carryout at same time?

36.4k Views Asked by At

In binary arithmetic, When you subtract 2 signed numbers you must discard the carry out. My question is, is it possible for overflow to occur and a carry out? So, on paper there would be two extra bits and you would have to discard one?

Also, if your subtracting 2 signed numbers, how do you differentiate between overflow and a carry out?

3

There are 3 best solutions below

0
On BEST ANSWER

When you subtract two signed numbers, the Carry flag is irrelevant; the Overflow flag is all that counts. The Carry flag is for unsigned integer operations. So there is only ever one bit to worry about.

3
On

Overflow and carry out are philosophically the same thing. Both indicate that the answer does not fit in the space available. The difference is that carry out applies when you have somewhere else to put it, while overflow is when you do not. As an example, imagine a four bit computer using unsigned binary for addition. If you try to add $1010_2+111_2$ without the word length restriction, you get $10001_2$ The high bit does not fit in our word. If this word is all the space allocated to this variable, it represents overflow as the result is too large to represent. If we have a two word space allocated to this variable, it becomes a carry out and is stored in the higher word. We would then represent the addition as $0000\ 1010_2+0000\ 0111_2=0001\ 0001_2$ and the carry is explicit.

0
On

You have 2 bits, C and V, so you have 4 options. I am going to use 8 bits for the number and other bit for the sign. With this sketch, we can represent numbers from -256 (1 0000 0000) to +255 (0 1111 1111). The first bit it is the sign: 0 it means positive and 1 means negative. Here are the options:

  1. Normal add (it doesn't go further the upper boundary 255) like (+120) + (+100). $$\begin{array}{rrr} (0 &0111 &1000)\\ + (0 &0110 &0100) \\ \hline (0 &1101 &1100) \end{array}$$ The bit sign didn't change, so the overflow becomes 0, and there is no carry present, so carry is 0.

  2. Normal subtract (it doesn't go further the lower boundary -255) like (-250) + (-5).

    To represent the -250 we have to do the complement plus 1. $$250 = 0\; 1111\; 1010 \\ \overline{250}= 1\; 0000\; 0101 \\ \overline{250}+1= 1\; 0000\; 0110$$ To represent the -5 we have to do the complement plus 1. $$5 = 0\; 0000\; 0101 \\ \overline{5}= 1\; 1111\; 1010 \\ \overline{5}+1= 1\; 1111\; 1011$$ And now we add $$\begin{array}{rrr}(&1& 0000& 0110)\\ + (&1 &1111& 1011)\\ \hline (1 &1 &0000 &0001) \end{array}$$ Where the first one is the carry, and the sign bit didn't change, so the overflow bit is 0. The result number is a negative number. To find the number we have to go backwards. $$ \overline{1\ 0000\ 0001} = 0\ 1111\ 1110 \\ \overline{1\ 0000\ 0001}+1 = 0\ 1111\ 1111 $$ So the result number is a negative 255.

  3. Addition over 255 (it goes above the upper boundary 255) like (+250) + (+6). So $$\begin{array}{rrr}(0 &1111 &1010)\\ + (0 &0000 &0101)\\ \hline (1 &0000 &0000)\end{array}$$ The addition of two numbers with the same sign results in a number with different sign what is wrong, so the overflow bit V is set to one, and there is no carry in this operation, so the flag C is 0. This operation it must return a error because the number it can't be represented.

  4. Subtract over -256 (it goes below the boundary -256) like (-250) + (-7).

    To represent the -250 we have to do the complement plus 1. $$250 = 0\; 1111\; 1010\\ \overline{250}= 1\; 0000\; 0101\\ \overline{250}+1= 1\; 0000\; 0110 $$ To represent the -7 we have to do the complement plus 1. $$7 = 0\; 0000\; 0111 \\ \overline{7}= 1\; 1111 \;1000 \\ \overline{7}+1= 1\; 1111\; 1001$$ And now we add $$\begin{array}{rrrr}(&1& 0000& 0110)\\ + (&1 &1111 &1001) \\ \hline (1& 0& 1111& 1111) \end{array}$$ Where the first one is the carry C, so the carry flag is set to 1, and the sign bit has changed, so the overflow bit is 1. This operation it must return a error because the number it can't be represented.