1

As far as I know, the two's complement algo is:

1.Represent the decimal in binary.
2.Inverse all bits.
3.Add 1 to the last bit.

For the number 3, which its representation is: 0000000000000011 the result of the two's complement would be 1111111111111101 which is -3.
So far so good. But for the number 2 which its representation is 0000000000000010 the result of the two's complement would be 1111111111111101, which isn't 2 but -3.
What am I doing wrong?

Lior
  • 5,841
  • 9
  • 32
  • 46

3 Answers3

3

For your code you might have needed to do 2's complement: i just wanted to throw this out there(a quicker way of getting a negative binary) :

2's complement is very useful for finding the value of a binary, however I thought of a much more concise way of solving such a problem(never seen anyone else publish it):

take a binary, for example: 1101 which is [assuming that space "1" is the sign] equal to -3.

using 2's complement we would do this...flip 1101 to 0010...add 0001 + 0010 ===> gives us 0011. 0011 in positive binary = 3. therefore 1101 = -3!

What I realized:

instead of all the flipping and adding, you can just do the basic method for solving for a positive binary(lets say 0101) is (23 * 0) + (22 * 1) + (21 * 0) + (20 * 1) = 5.

Do exactly the same concept with a negative!(with a small twist)

take 1101, for example:

for the first number instead of 23 * 1 = 8 , do -(23 * 1) = -8.

then continue as usual, doing -8 + (22 * 1) + (21 * 0) + (20 * 1) = -3

Hope that may help!

Simon Yundov
  • 63
  • 1
  • 9
1
0...0010 // 2
1...1101 // Flip the bits
1...1110 // Add one

It works for negative too:

1...1110 // -2
0...0001 // Flip the bits
0...0010 // Add one
James
  • 9,064
  • 3
  • 31
  • 49
  • Thank you, I had a mistake in the addition of the 1. – Lior Dec 06 '12 at 01:31
  • @pst I don't know what you're saying – James Dec 06 '12 at 01:32
  • I have another mini-question. What is the difference between my algorithm of negating a number and scanning the number from right to left and complementing all the bits after the first appearance of a 1? – Lior Dec 06 '12 at 01:34
  • @Lior I'm not sure it would work but you could try it out with pen and paper. Even if it did I assume the circuitry required would be far more that a simple bit inversion and add, which is very quick. – James Dec 06 '12 at 01:39
  • Ok, thank you :) If you want to read more about the "alternative conversion process" (as written in wikipedia) : http://en.wikipedia.org/wiki/Two%27s_complement – Lior Dec 06 '12 at 01:44
  • @Lior: It is mathematically same. The effect of adding 1 in the 3rd step will re-flip low bits to zero or more zeroes (`0`) preceded by a one (`1`); so instead of flipping everything and then reflipping low bits by addition of `1`, you can just flip those that would flip anyway, and not flip those that would be reflipped by addition. The algorithm (up to first `1` from the right) tells you which bits would be affected by the step 3. In case of `2` you had, adding `1` to `1101` flips to `1110`, so the last `10` is the nonflipping portion from original `0010`; everything else to the left flips. – Amadan Dec 06 '12 at 01:56
0

What am I doing wrong?

Skipping step 3 for your second example (or misunderstanding it).

1111111111111101 is ones' complement of 2 (i.e. result of step 1 and 2); you need to add 1 - not to the last bit (as in binary digit), but to the last result (as in, what you get from step 2).

Amadan
  • 191,408
  • 23
  • 240
  • 301