# two's compliment math

J

#### JJ

Jan 1, 1970
0
Have been trying to do 32bit signed subtraction using two's compliment but I
can't seem to get correct figures if either (or both)of the original
operands are negative.
For a simple example in real math, 3 subtract -2 is 5, but the two's
compliment routines say the result is -1.
Am I missing something or is two's compliment unsuitable for mixed sign
subtraction?
The routines are public domain and work precisely for two positive operands
so it appears the routines are working correctly.
Do I need to go to another form of subtraction(fixed/floating point)?
What I really want is to find a quick way to find the difference between two
32bit signed numbers.
Anyone?
Thanks
JJ

D

#### david

Jan 1, 1970
0
Have been trying to do 32bit signed subtraction using two's compliment but
I
can't seem to get correct figures if either (or both)of the original
operands are negative.
For a simple example in real math, 3 subtract -2 is 5, but the two's
compliment routines say the result is -1.

two compliment is very basic to computers and also its about ten years ago
in my memory but I seem to remember the last thing you needed to was add 1

J

#### JJ

Jan 1, 1970
0
david said:
two compliment is very basic to computers and also its about ten years ago
in my memory but I seem to remember the last thing you needed to was add 1

that to the other operand which works just fine but my question was can
two's compliment deal with all signs at both ends of the equation...the code
I have say's apparently not.
JJ

D

#### david

Jan 1, 1970
0
Yes, you negate one operand and add i.e. invert all bits, add 1 and then
that to the other operand which works just fine but my question was can
two's compliment deal with all signs at both ends of the equation...the
code
I have say's apparently not.
JJ

well JJ looks like you know the answer
someone else might pop up with a solution.
hope you get it sorted.

David

N

#### News 2 Me

Jan 1, 1970
0
JJ said:
Have been trying to do 32bit signed subtraction using two's compliment but I
can't seem to get correct figures if either (or both)of the original
operands are negative.
For a simple example in real math, 3 subtract -2 is 5, but the two's
compliment routines say the result is -1.
Am I missing something or is two's compliment unsuitable for mixed sign
subtraction?
The routines are public domain and work precisely for two positive operands
so it appears the routines are working correctly.
Do I need to go to another form of subtraction(fixed/floating point)?
What I really want is to find a quick way to find the difference between two
32bit signed numbers.
Anyone?

I don't know what your routines are doing, but

3 = 00000011
-2 = 11111110

To subtract b from a, you add the two's compliment of b to a:

3 = 00000011
--2 = 00000010
--------------
5 = 00000101

Are you sure you are not adding 3 and -2 together..?

NM

N

#### News 2 Me

Jan 1, 1970
0
News said:
I don't know what your routines are doing, but

3 = 00000011
-2 = 11111110

To subtract b from a, you add the two's compliment of b to a:

3 = 00000011
--2 = 00000010

What I meant to say was:

Are you sure you're not subtracting -2 from -3?

-3 - (-2) = -1

NM

S
Replies
2
Views
2K
steve
S
P
Replies
1
Views
966
Nukie Poo
N
Replies
1
Views
145
Replies
3
Views
665
Replies
2
Views
785