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?
Two's complement for addition and subtraction
Page 1 of 16 Replies - 4355 Views - Last Post: 04 November 2013 - 09:02 PM
Replies To: Two's complement for addition and subtraction
#2
Re: Two's complement for addition and subtraction
Posted 04 November 2013 - 03:31 PM
streek405, 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?
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
streek405, 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.
#3
Re: Two's complement for addition and subtraction
Posted 04 November 2013 - 08:15 PM
Skydiver, 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
Skydiver, on 04 November 2013 - 03:31 PM, said:
Would I take the twos complement of the answer then? Or do ONLY add it normally?
#4
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
#5
Re: Two's complement for addition and subtraction
Posted 04 November 2013 - 08:34 PM
GunnerInc, 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...
#6
Re: Two's complement for addition and subtraction
Posted 04 November 2013 - 08:42 PM
GunnerInc, 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!
#7
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
http://courses.cs.vt...ment/index.html
Page 1 of 1

New Topic/Question
Reply


MultiQuote





|