# Arithmetic with signed binary

1. Sep 27, 2011

### geft

Why is it that when using 2's complement, the result of arithmetic operations differ by two?

11011001 (-39) +
11100111 (-25) =
11000000 (which is -62 in 2's complement, even though it's supposed to be -64)

00110011 (51) +
11101110 (-16) =
00100001 (33, but it's supposed to be 35)

2. Sep 27, 2011

### lewando

Some errors:

-64 is 11000000 in 2's complement.

-16 is 11110000 in 2's complement.

3. Sep 27, 2011

### geft

Am I doing it wrong?
11000000
=> 00111110
= 2^1 + 2^2 + 2^3 + 2^4 + 2^5 = 62
=> -62

11110000
=> 00001110
= 2^1 + 2^2 + 2^3 = 14
=> -14

4. Sep 27, 2011

### eumyang

Looks like you're subtracting one after inverting the bits? I believe you always add one, so it should be
00111111 + 1 = 01000000

and
00001111 + 1 = 00010000

5. Sep 27, 2011

### lewando

Agree with eumyang.

The algorithm for dealing with negative numbers (positives are easy--do nothing) is this :

To convert, say -64 into 8-bit 2's complement:
1) Take the 8-bit binary of the positive part (64): 0100 0000
2) Since the original value is negative, complement the result from 1) and add 1: 1011 1111 + 1 = 1100 0000

To find the decimal given the 8-bit 2's complement value, say for 1100 0000:
1) Note that the MSB is a 1 and therefore your result will be a negative number.
2) Complement the original value: 1100 0000 => 0011 1111
3) Add 1: 0011 1111 + 1 = 0100 0000 (which is binary 64)
4) Apply the negative sign from part 1) to the result from part 3) "-" : 64 = -64

6. Sep 28, 2011

### geft

I see where I was wrong... I thought I didn't need to change the last bit in any way, so when inverting I always leave the last bit alone. Thanks!