You are on page 1of 3

Why is two's complement used to represent negative numbers?

Ans 1) It's done so that addition doesn't need to have any special logic for dealing with negative numbers. Check out the article on Wikipedia. ay you have two numbers! " and #1. In your $intuitive$ way of representing numbers! they would be 0010 and 1001! respectively %I'm sticking to & bits for si'e). In the two's complement way! they are 0010 and 1111. (ow! let's say I want to add them. )wo's complement addition is very simple. *ou add numbers normally and any carry bit at the end is discarded. o they're added as follows+
0010 + 1111 =10001 = 0001 (discard the carry) 0001

is 1! which is what we e,pected the result of $"-%#1)$ to be.

.ut in your $intuitive$ method! adding is more complicated+


0010 + 1001 = 1011

Which is #/! right0 imple addition doesn't work in this case. *ou need to note that one of the numbers is negative and use a different algorithm if that's the case. 1or this $intuitive$ storage method! subtraction is a different operation than addition! re2uiring additional checks on the numbers before they can be added. ince you want the most basic operations %addition! subtraction! etc) to be as fast as possible! you need to store numbers in a way that lets you use the simplest algorithms possible. Additionally! in the $intuitive$ storage method! there are two 'eroes+
0000 1000 "zero" "negative zero"

Which are intuitively the same number but have two different values when stored. 3very application will need to take e,tra steps to make sure that non#'ero values are also not negative 'ero. )here's another bonus with storing ints this way! and that's when you need to e,tend the width of the register the value is being stored in. With two's complement! storing a &#bit number in a 4#bit register is a matter of repeating its most significant bit+
0001 (one, in four bits) 00000001 (one, in eight bits)

1110 (negative two, in four bits) 11111110 (negative two, in eight bits)

It's 5ust a matter of looking at the sign bit of the smaller word and repeating it until it pads the width of the bigger word. With your method you would need to clear the e,isting bit! which is an e,tra operation in addition to padding+
0001 00000001 1010 10000010 (one, in four bits) (one, in eight bits) (negative two, in four bits) (negative two, in eight bits)

*ou still need to set those e,tra & bits in both cases! but in the $intuitive$ case you need to clear the 6th bit as well. It's one tiny e,tra step in one of the most fundamental and common operations present in every application. Ans ") )wo's complement allows negative and positive numbers to be added together without any special logic. If you tried to add 1 and #1 using your method 17777771 %#1) -77777771 %1) you get 17777717 %#") Instead! by using two's complement! we can add 11111111 %#1) -77777771 %1) you get 77777777 %7) )he same is true for subtraction. Also! if you try to subtract & from 8 %two positive numbers) you can "'s complement & and add the two together 8 - %#&) 9 8 # & 9 " )his means that subtraction and addition of both positive and negative numbers can all be done by the same circuit in the cpu. Ans /) )he usual implementation of the operation is $flip the bits and add 1$! but there's another way of defining it that probably makes the rationale clearer. "'s complement is the form you get if you take the usual unsigned representation where each bit controls the ne,t power of "! and 5ust make the most significant term negative. )aking an 4#bit value a: a8 a6 a& a/ a" a1 a7

)he usual unsigned binary interpretation is+ ":a7 + 26a8 - ; "6a5 + 24a& - "/a3 +22a" - "1a1 + 20a7 11111111 9 1"4 - 8& - /" - 18 - 4 - & - " - 1 9 "66 )he two's complement interpretation is+ #":a7 + 26a8 - ; "6a5 + 24a& - "/a3 +22a" - "1a1 + 20a7 11111111 9 #1"4 - 8& - /" - 18 - 4 - & - " - 1 9 #1 (one of the other bits change meaning at all! and carrying into a: is $overflow$ and not e,pected to work! so pretty much all of the arithmetic operations work without modification %as others have noted). ign#magnitude generally inspect the sign bit and use different logic. Ans &) Well! your intent is not really to reverse all bits of your binary number. It is actually to subtract each its digit from 1. It's 5ust a fortunate coincidence that subtracting 1 from 1 results in 7 and subtracting 7 from 1 results in 1. o flipping bits is effectively carrying out this subtraction. .ut why are you finding each digit's difference from 10 Well! you're not. *our actual intent is to compute the given binary number's difference from another binary number which has the same number of digits but contains only 1's. 1or e,ample if your number is 17117771! when you flip all those bits! you're effectively computing %11111111 # 17117771). )his e,plains the first step in the computation of )wo's Complement. (ow let's include the second step ## adding 1 ## also in the picture. Add 1 to the above binary e2uation+ 11111111 # 17117771 - 1 What do you get0 )his+ 177777777 # 17117771 )his is the final e2uation. And by carrying out those two steps you're trying to find this! final difference+ the binary number subtracted from another binary number with one e,tra digit and containing 'eros e,cept at the most signification bit position.

You might also like