0

I understand that when subtracting binaries you should convert the second binary to it's 2's Complement. But in the following case:

                 01101+11110

The 11110 was converted to its 2's Complement. In other words the working equation is now:

                  01101+00010

Now I am pretty confused on when I should be converting. Any help would be greatly appreciated!

Monsi
  • 43
  • 5
  • when you should be converting? You’ve already said that when subtracting you should convert –  Oct 18 '19 at 02:51
  • @AkutaHinako Yes I know that when subtracting I have to convert. but a classmate of mine insists that the case above 11110 should be converted to its 2's Complement first before adding it. Is that correct or you just add it straight away? – Monsi Oct 18 '19 at 02:53
  • For the above case you may notice it has algebra overflow. –  Oct 18 '19 at 02:56
  • So if there is an overflow, do I have to to convert it first since in decimal notation it is 13-2=+ so the answer above should be positive??? – Monsi Oct 18 '19 at 02:58
  • the computer will straight add them together and abandon the carry so just add them together is ok –  Oct 18 '19 at 03:01
  • No the answer is undefined since overflow –  Oct 18 '19 at 03:05
  • Adding them together as is would give me 01011 with an overflow 1 which is equal to 11 in decimal which is equal to 13-2. So I think my classmate is wrong, am I right? – Monsi Oct 18 '19 at 03:08
  • For signed number 01101 is 13 and 11110 is -2. Adding them gives correct answer 11. But if it’s unsigned then it’s overflow and the result is undefined. That’s all depending on whether you want to do signed operation or unsigned. –  Oct 18 '19 at 03:15
  • For signed 01101 is 13 and 11110 Is -2. But for unsigned 11110 is 30 and you are operating 30+13. But 43 is too large to store in 5 bit binary, therefore it’s overflow and giving undesired result –  Oct 18 '19 at 03:19
  • Sorry for that but I am not Japanese, I’m Chinese. and I am also new in programming I’ve just started learning. Sorry for having not given the precise answer at the first time answering. –  Oct 18 '19 at 03:23
  • Oh I see, my bad sorry for the assumption. I am in the same boat, well in any case goodluck to the both of us! And thank you for the help! – Monsi Oct 18 '19 at 03:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201060/discussion-between-akuta-hinako-and-monsi). –  Oct 18 '19 at 03:28

2 Answers2

0

What I commented maybe not precise enough, though. I mistake CARRY for OVERFLOW.

-

So explain it little further

Your operation is:

01101 (binary) + 11110 (binary) = 01011 (binary)

-

if signed (CORRECT RESULT):

  13 (decimal) +   -2 (decimal) =    11 (decimal)

if unsigned (UNDESIRED RESULT):

  13 (decimal) +   30 (decimal) =    11 (decimal)

-

There are two things in a binary arithmetic operation called separately OVERFLOW and CARRY.

For addition operation, The CARRY flag is set if the addition of two numbers causes a carry out of the most significant (leftmost) bits added.

example:

1111 + 0001 = 0000 (carry flag is turned on)

0111 + 0001 = 1000 (carry flag is turned off)

For subtraction operation, The CARRY flag is set if the the subtraction of two numbers requires a borrow into the most significant (leftmost) bits subtracted.

example:

0000 - 0001 = 1111 (carry flag is turned on)

1000 - 0001 = 0111 (carry flag is turned off)

For addition operation, the OVERFLOW flag is set if the addition of two numbers with the same most significant bit (both 0 or both 1) and the result produced a binary with a different most significant bit.

example:

0100 + 0100 = 1000 (overflow flag is turned on)

0100 + 0001 = 0101 (overflow flag is turned off)

-

Note that:

In unsigned arithmetic, watch the carry flag to detect errors.

In signed arithmetic, the carry flag tells you nothing interesting.

-

In signed arithmetic, watch the overflow flag to detect errors.

In unsigned arithmetic, the overflow flag tells you nothing interesting.

-

check error in adding unsigned binaries in assembly language

ADD BX, AX
//check carry here

check error in subtracting unsigned binaries in assembly language

SUB BX, AX
//check carry here

check error in adding signed binaries in assembly language

ADD BX, AX
//check overflow here

check error in subtracting signed binaries in assembly language

SUB BX, AX
//check overflow here

See this for detail of the flag sets in ADD/SUB

carry/overflow & subtraction in x86

0

As you have understood correctly you want to do a twos complement. Logic will use addition to do subtraction by taking advantage of the beauty of twos complement.

One of the ways we are taught to "take the twos complement" is to invert (ones complement) and add one.

So if you want to do 4 - 7 (0b00100 - 0b00111)

That is the same as 0b00100 + 0b11000 + 1, can take advantage of the carry in of the lsbit:

      1
  00100
+ 11000
========

Do the grade school math in base 2.

 000001
  00100
+ 11000
========
  11101

which is -3 if this is interpreted as a signed operation (same add/subtract logic for signed and unsigned, processor doesn't know, only the programmer does).

I assume instead of

01101+11110

you wanted to subtract those two numbers

01101-11110 13 - (-2) if interpreted as signed numbers

 000011
  01101
+ 00001
========
  01111

13 + 2 = 15

the carry in and the carry out of the msbit are the same so there is no signed overflow the result is good. Note there isn't an unsigned overflow either.

So for subtraction you invert the carry in and the second operand from what you would for addition. Some processor architectures invert the carry out to indicate a borrow, some don't. You have check the architecture of the processor you are using to know if yours does. Unfortunately few do a good job at that level of documentation so sometimes you have to figure it out experimentally.

For addition you do not invert any of the operands into the adder. The operation (add vs sub) determines whether or not you invert on the way into the adder.

halfer
  • 19,824
  • 17
  • 99
  • 186
old_timer
  • 69,149
  • 8
  • 89
  • 168