Binary Addition (2s Complement)
Date: 09/25/2001 at 19:22:53 From: Paul Subject: Binary addition (2s complement) Dear Dr. Math, I was given a number in binary (10110111), and asked to convert first to decimal using 2s complement, then to BCD (binary coded decimal). My problem is in the understanding of how 2s complement works. Could you help me? Paul
Date: 09/26/2001 at 03:43:51 From: Doctor Jeremiah Subject: Re: Binary addition (2s complement) Hi Paul, 2's complement was invented just so that binary numbers could be negative. If a binary number is positive, then 2's complement does nothing. So how do we know if a 2's complement binary number is negative? Well, it depends on how many digits you are supposed to use. The reason this makes a difference is that if you are using 16-digit binary numbers, a number is negative if the 16th digit (the one on the left) is 1. So in this case your number (10110111) is actually 0000000010110111 and does not have a 1 in the 16th digit. However, if you are using 8-digit binary numbers, then your number (10110111) does have a 1 in the eighth digit (far left), and your number will be negative in this case. Lets assume that the binary numbers are 8 digits long. That way your number is negative and far more interesting. When you subtract x from y using the 2's complement rules, you basically just invert all the bits of x, add one to x, and then add that to y instead of subtracting. For example: 00000000 00000000 00000000 - 00000001 ===> + 11111110+1 ===> + 11111111 So 00000000 - 00000001 (positive) = 11111111 (negative number) To find the value, you convert it to a positive number and then put a minus sign in front of it. So what about your number (10110111)? Well if we do the same thing in reverse: 1. Take 10110111 and subtract 1 = 10110110 2. Take 10110110 and invert the bits = 01001001 This number (01001001) is the positive value that would have to be subtracted from 0 to give you 10110111. So 00000000 - 01001001 (positive) = 10110111 (negative number) 00000000 00000000 00000000 - 01001001 ===> + 10110110+1 ===> + 10110111 It's relatively easy to convert 01001001 to decimal because there is nothing special to do: 0 (00000000) - 73 (01001001) = -73 (10110111). - Doctor Jeremiah, The Math Forum http://mathforum.org/dr.math/
Search the Dr. Math Library:
Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.