views:

775

answers:

4

Hi, I have f.eks
X = 01001001 and Y = 10101010

If I want to add them together how do I do that? They are "Two's Complement"... I have tried a lots of things but I am not quite sure I am getting the right answer since there seems to be different type of rules.

Just want to make sure it is correct:
1. Add them as they are do not convert the negative
2. Convert the negative number you get and that's the sum.

f.eks
01001001+10101010 = 11110011 => 00001100 => 1101 => -13

Or?
1. Convert the negative
2. Add them together and convert the negative

f.eks
01001001+10101010 => 01001001 + 01010110 => 10011111 => 01100001 => -97


So basically what I want to do is to take: X-Y, and X+Y
Can someone tell me how to do that?

Some resource sites: student-binary celtickane swarthmore

+1  A: 

Adding in two's complement doesn't require any special processing when the signs of the two arguments are opposite. You just add them as you normally would in binary, and the sign of the result is the sign you keep.

Bill the Lizard
+9  A: 

The beauty of two's complement is that at the binary level it's a matter of interpretation rather than algorithm - the hardware for adding two signed numbers is the same as for unsigned numbers (ignoring flag bits).

Your first example - "just add them" - is exactly the right answer. Your example numbers

  • 01001001 = 73
  • 10101010 = -86

So, the correct answer is indeed -13.

Subtracting is just the same, in that no special processing is required for two's complement numbers: you "just subtract them".

Note that where things get interesting is the handling of overflow/underflow bits. You can't represent the result of 73 - (-86) as an 8-bit two's complement number...

Roddy
But if subtracting is the same as adding them? Then wont I get the same answer?
suxSX
Subtraction for unsigned numbers is the same as subtraction for signed numbers.
Artelius
Ah, so I follow the same rule for subtracting and convert in the end as I did with the addding part :)
suxSX
There's no "conversion" needed at the end. The value you got "11110011" simply 'is' -13.
Roddy
subtracting is not the 'same' as adding, it;s the same as subyrcating any other numbers, Just subtract each digit (each bit) on the bottom from the corresponding bit on the top, borrrowing from the bit to the left if you're tryinmg to subtract 1 from 0... same as you would for a decimal subtraction
Charles Bretana
This only works because the unsigned number's MSB is 0. I added an answer below about that.
Nathan Fellman
+1  A: 

And just to make sure you understand two's complement, to convert from a positive to a negative number (or vice versa): invert each bit, then add 1 to the result.

For example, your positive number X = 01001001 becomes 10110101+1=10110110 as a negative number; your negative number Y = 10101010 becomes 01010101+1=01010110 as a positive number.

To subtract Y from X, negate Y and add. I.E. 01001001 + 01010110.

Mark Ransom
+1  A: 

Your confusion might be because of the widths of the numbers involved. To get a better feel for this you could try creating a signed integer out of your unsigned integer.

If the MSB of your unsigned integer is already 0, then you can read it as signed and get the same result.
If the MSB is 1 then you can append a 0 to the left to get a signed number. You should sign-extend (that is, add 0s if the MSB is 0, add 1s if the MSB is 1) all the signed numbers to get a number of the same width so you can do the arithmetic "normally".

For instance, using your numbers:

X = 01001001: Unsigned, MSB is 0, do nothing.

Y = 10101010: Signed, did nothing with X, still do nothing.

But if we change the MSB of X to 1:

X = 11001001: Unsigned, MSB is 1, Add a 0 --> 011001001

Y = 10101010: Signed, extended X, so sign-extend Y --> 110101010

Now you have two signed numbers that you can add or subtract the way you already know.

Nathan Fellman