Adding negative and positive binary?

X = 01001001 and Y = 10101010

If I want to add them together, how do I do this? This is “Two Additions” ... I tried a lot of things, but I'm not quite sure that I am getting the right answer, because there seem to be different rules.

Just want to make sure this is correct:
1. Add them as they do not convert negative
2. Convert the negative number that you get, and that amount.

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

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

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


So basically I want to do: XY and X + Y
Can someone tell me how to do this?

Some resource sites: student-binary celtickane swarthmore

+4
source share
4 answers

The beauty of the two additions is that at the binary level it is a matter of interpretation, not an algorithm - the hardware for adding two signed numbers is the same as for unsigned numbers (ignoring bit-bits).

Your first example, “just add them,” is exactly the right answer. Your sample numbers

  • 01001001 = 73
  • 10101010 = -86

So the correct answer is really -13.

Subtraction is the same, since two addition numbers do not require special processing: you simply subtract them.

Note that in cases where an interesting thing is handling the overflow / overflow bits. You cannot imagine the result 73 - (-86) as an 8-bit binary number ...

+12
source

Adding to two additions does not require special processing when the signs of two arguments are opposite. You simply add them, as usual, in binary format, and the sign of the result is the character that you store.

+1
source

And just to make sure you understand the two additions, 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, cancel Y and add. I.E. 01001001 + 01010110.

+1
source

Your confusion may be due to the width of the numbers involved. To better understand this, you can try to create a signed integer from your unsigned integers.

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

For example, using your numbers:

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

Y = 10101010: Signed, did nothing with X, does nothing.

But if we change the MSB X to 1:

X = 11001001: Unsigned, MSB - 1, add 0 → 011001001

Y = 10101010: Signed, extended X, so the extension sign Y → 110101010

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

+1
source

All Articles