6 Replies - 4355 Views - Last Post: 04 November 2013 - 09:02 PM

#1 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Two's complement for addition and subtraction

Posted 04 November 2013 - 02:45 PM

I was wondering what the rules are for subtracting or adding when using a 2's compliment. I searched online and for subtraction it said to complement only one of the numbers and then add them together. So for example:

1011 - 0011
compliment the 2nd value, which would turn into: 1101
so now you have: 1011 + 1101
is this true?

And as for addition, would you just take the twos comp of both the numbers and add them?

Is This A Good Question/Topic? 0
  • +

Replies To: Two's complement for addition and subtraction

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Two's complement for addition and subtraction

Posted 04 November 2013 - 03:31 PM

View Poststreek405, on 04 November 2013 - 04:45 PM, said:

I was wondering what the rules are for subtracting or adding when using a 2's compliment. I searched online and for subtraction it said to complement only one of the numbers and then add them together. So for example:

1011 - 0011
compliment the 2nd value, which would turn into: 1101
so now you have: 1011 + 1101
is this true?


In the time it took you to type up your question, you could have given it a try yourself...
1011b = 11
0011b = 3

11 - 3 = 8

1101b = 13

11 + 13 == 24

24 = 11000b

The leading 1 is kept in the carry bit since we can only hold 4 bits. So we have:
1000b = 8

View Poststreek405, on 04 November 2013 - 04:45 PM, said:

And as for addition, would you just take the twos comp of both the numbers and add them?

No, you just add them.
Was This Post Helpful? 0
  • +
  • -

#3 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Two's complement for addition and subtraction

Posted 04 November 2013 - 08:15 PM

View PostSkydiver, on 04 November 2013 - 03:31 PM, said:

In the time it took you to type up your question, you could have given it a try yourself...


Believe me that is exactly what I did, but I wasnt sure. Plus, I still dont quite understand how to subtract if I have to "borrow" from many numbers.

For example:

1011 0100 (180)
-0111 0111 (119)

I get 0100 0101, which is not the correct answer(61)

How do I approach this? Since the last bit, 0, is smaller than 1, it has to borrow from the 2nd to last bit which would then turn into 1. And just repeat the process, but I guess this is wrong because my answer is not 61

View PostSkydiver, on 04 November 2013 - 03:31 PM, said:

View Poststreek405, on 04 November 2013 - 04:45 PM, said:

And as for addition, would you just take the twos comp of both the numbers and add them?

No, you just add them.

Would I take the twos complement of the answer then? Or do ONLY add it normally?
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 931
  • View blog
  • Posts: 2,376
  • Joined: 28-March 11

Re: Two's complement for addition and subtraction

Posted 04 November 2013 - 08:20 PM

Maybe this will make it more clear: wikiHow - How to Subtract Binary Numbers
Was This Post Helpful? 0
  • +
  • -

#5 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Two's complement for addition and subtraction

Posted 04 November 2013 - 08:34 PM

View PostGunnerInc, on 04 November 2013 - 08:20 PM, said:

Maybe this will make it more clear: wikiHow - How to Subtract Binary Numbers

I checked out that link before but it seems to be contradicting for the 3rd step. It states to take the two's compliment of the the SECOND number, but in their example, they take the two's comp to both the first and second numbers. Then, on step 4, they add the sum of the number to itself...
Was This Post Helpful? 0
  • +
  • -

#6 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Two's complement for addition and subtraction

Posted 04 November 2013 - 08:42 PM

View PostGunnerInc, on 04 November 2013 - 08:20 PM, said:


I kept searching and I found this video: http://www.youtube.c...h?v=h_fY-zSiMtY

I think it does a pretty good job of explaining how to subtract normally. I was doing it completely wrong!
Was This Post Helpful? 0
  • +
  • -

#7 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Two's complement for addition and subtraction

Posted 04 November 2013 - 09:02 PM

I found a link that explains subtraction for twos compliment very well if anyone's interested. It really helped me out:

http://courses.cs.vt...ment/index.html
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1