How to prove this theory I found out? Quicker way of doing 2's complement(finding out the value of a negative binary)

StackOverflow https://stackoverflow.com/questions/17287668

Pergunta

I was playing around with 2's complement and found a quicker way of finding the value of a negative binary. Please help me prove this(right or wrong) or why it works! Thanks in advance!

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

Note: this seemed to work for every test I did.

Thanks for looking. Also, if you haven't seen this before please let me know, I never seen this anywhere.

Foi útil?

Solução

This solution seems to work for the tests which I did! Thanks for the quick solution to 2's complement, I will still test out some more difficult questions.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top