0
\$\begingroup\$

In base 2, I want to subtract x-y using adder. Where, x = (1011)2 and y = (0101)2

[For verification, in decimal x=(11)10, y = (5)10. So, we are seeking (6)10 as the answer ]

In base 2 using adder we are looking for (1011)2-(0101)2 = (?)2

Or, actually we are looking for (1011)2+[-(0101)]2 = (?)2

Procedure: Step 1: Find 1's complement of y = (0101)2

(1111)2-(0101)2 = (1010)2

Step 2: I'll add 1 to get 2's complement

(1010)2 + (1)2 = (1011)2 --- result 1

Step 3: Now, I'll add x = (1011)2 to result 1 which is 2's complement of y = (0101)2

(1011)2 + (0101)2 = (10110)2 --- result 2

Here, in (10110)2 we get End Around Carry which happens to be 1 at most significant bit in (10110)2.

We remove End Around Carry from the result 2 and get the answer (0110)2 = (6)10

So, finally, my questions here is to know following:

  1. Even though, we want to carry subtraction using addition, we still have to carry subtraction to find 1's complement [as show in step 1]. So, we still require subtractor to carry 1' complement. If that is the case, then why we take pain to carry unnecessary procedure of subtracting numbers using adder (addition)?
  1. Or, please let me know, how actually, subtraction is carried using adder in absence of subtractor?
\$\endgroup\$
2
  • 3
    \$\begingroup\$ In two's complement, \$1011_2\$ is not equal to decimal 11. Your binary addition for result 2 is also incorrect. \$\endgroup\$ Commented Jun 26, 2021 at 2:24
  • \$\begingroup\$ @ElliotAlderson I see where is the problem, I have edited the opening phrase reading 2'complement. It is binary subtraction in base 2 using adder. But in process you have to find 2's complement of y (which y prime or -y). So, I used 2's complement to find y prime. Number 11 is represented in base 2 only. \$\endgroup\$
    – Ubi.B
    Commented Jun 26, 2021 at 2:31

1 Answer 1

1
\$\begingroup\$

Using an adder makes it easier than using a subtractor, less logic, easier to understand, etc.

In grade school we learned a - b = a + (-b). And our first computer class we learned about this thing called twos complement and that to get the "twos complement" you invert and add one. So a - b = a + (-b) = a + (~b) + 1

    1  plus one
 1011  a
+1010  ~b
=====

fill it in

10111
 1011
+1010
=====
 0110

1011 - 0101, (unsigned) 12-5 = 6

1011 - 0101, (signed) -5 -5 = -10 (which cannot be represented in 4 bits)

So unsigned this is good, the carry out indicates not borrow, some architectures invert the carry out into the carry bit/flag indicating a borrow, some do not invert.

For signed numbers we see that the carry in and carry out of the msbit do not match (or another way to see it is the msbits match but the result doesnt) so this is a signed overflow the result will not fit in four bits.

So lets try 5 bits

11011 - 00101  -5 - 5 = -10

      1
  11011
+ 11010
========

 110111
  11011
+ 11010
========
  10110

(~10110)+1 = 01001 + 1 = 01010

And there we go, the carry out indicates not borrow, which you use to look for greater than less than. And we do not have a signed overflow. So although you might not have asked this this shows both signed and unsigned work using an adder.

Super trivial to use an adder to do subtraction, this is the beauty of ones complement. Using an adder to do subtraction is beautiful and simple.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Ones complement means invert all the bits. The ones complement of 0101 is 1010 the ones complement of 1100 is 0011, not sure how you would use subtraction to do ones compliment, sounds painful. And twos complement means invert and add one. Subtraction is nowhere to be seen. \$\endgroup\$
    – old_timer
    Commented Jun 26, 2021 at 3:13
  • 2
    \$\begingroup\$ Yes ones complement means invert all the bits. \$\endgroup\$
    – old_timer
    Commented Jun 26, 2021 at 3:15
  • \$\begingroup\$ how you would use subtraction to do ones compliment, sounds painful I have shown it in step 1. You have to subtract 0101 from 1111 i.e. 1111 - 0101 = 1010. I am CS student, so I stumble upon the thought that how 1's complement is carried out internally using adder. Because according to the process taught to me, I have to do 1111-0101 = 1010 in order to flip the bits. Instead, I could have thought by just toggling/flipping all digits. \$\endgroup\$
    – Ubi.B
    Commented Jun 26, 2021 at 3:24
  • \$\begingroup\$ en.wikipedia.org/wiki/Ones%27_complement "The ones' complement of a binary number is the value obtained by inverting all the bits in the binary representation of the number" \$\endgroup\$
    – old_timer
    Commented Jun 26, 2021 at 3:27
  • 1
    \$\begingroup\$ As a CS student they should have taught you about ones complement early on, and that it is flipping each bit...and that twos complement is "invert and add one" (as well as subtracting from zero naturally) \$\endgroup\$
    – old_timer
    Commented Jun 26, 2021 at 3:29

Your Answer

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

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