About 2's Complement

Apr 13, 2012 at 4:43pm
So, I was reading about 2's complement today and it got me wondering, how does that thing works? To be a little precise, we know that adding two binary numbers like A + B will actually give us A - B provided that B is in 2's complement form.
So what is the secret (maths actually) behind that thing? Anybody?
Apr 13, 2012 at 7:07pm
A+B is not A-B, any way you look at it.

Arithmetic in two's complement is normal arithmetic of natural numbers that drops the first digit if it doesn't fit. 1111 is 15. 1111 + 1 = 10000, but, assuming 4 bits only 0000 fits into the result, thus 1111 + 1 = 0. For that reason 1111 could be called -1. Addition doesn't really care what you call things.
Apr 13, 2012 at 7:29pm
A+B is not A-B, any way you look at it

Unless it's modulo 2 (aka binary) ;)

Anyways, who cares about binary -Anyways, I refuse to use any counting system other than the decimal.

The math behind computation mod 2: In modulo 2 we have that 2=0, and we can see that:

A-B = A+(-1)*B = A+1*B = A+B (mod 2)

because
1
2
1= 1-0 = 1-2        (mod 2) 
1=-1                (mod 2)
Last edited on Apr 13, 2012 at 7:40pm
Apr 14, 2012 at 6:36am
Two's complement is not modulo 2, it's modulo 2n.
Apr 14, 2012 at 7:55am
modulo 2n?
Apr 14, 2012 at 11:55am
Where n is the number of bits. Assuming n = 4, as in previous example, 15+4 = 19, 19 mod 16 = 3, so 15+4 = 3 (mod 24).
Topic archived. No new replies allowed.