Two's Complement Misunderstanding

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Two's Complement Misunderstanding

Andreas
Hi,

In the book (page 31) two's complement of x is said to be calculated by (2^n)-x. It then goes on to say that to calculate -2 you perform the following operation: (2^4)-2. However, why if x=-2 in this case is it (2^4)-2 and not (2^4)-(-2)? Also, to calculate two's complement where x=2, one would perform the operation: (2^4)-(-2) in order to yield the result 0010.

I am very confused...

Can anyone explain?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Two's Complement Misunderstanding

cadet1620
Administrator
It's those pesky minus signs, curse of Algebra students worldwide...  Also, there are too many twos to keep straight; I'm going to use 3.

The binary representation of -3 is the two's complement of +3.
The 4-bit two's complement of +3 is 2^4 - (+3) which is 13, so -3 is 1101 in 4-bit binary.

Overly picky mathematicians will tell us that two's complements only exist for positive numbers, but we're programmers and engineers so we don't listen to them.

The 4-bit two's complement of -3 is 2^4 - (-3) = 19 = 10011 and we throw away the overflow and get 0011, the 4-bit binary representation of +3.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Two's Complement Misunderstanding

David Eisner
I think Andreas is right to be confused, because the text fails to clearly distinguish between "the 2's complement of the number x" and "the 2's complement representation of the number x".  

An additional sentence in the text is needed at the beginning of the "For example ..." paragraph.  This sentence might read, "The 2's complement representation of a negative number x is the 2's complement of it's absolute value, |x|."  

Just my 10 cents.
Reply | Threaded
Open this post in threaded view
|

Re: Two's Complement Misunderstanding

apcheBoy
Thanks David for the clarification. This indeed started throwing me off the track a bit ;)