# two's compliment math

#### Do you have a question? Post it now! No Registration Necessary

•  Subject
• Author
• Posted on

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

Re: two's compliment math

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

Re: two's compliment math

but

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

Re: two's compliment math

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

David

Re: two's compliment math

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

Re: two's compliment math

What I meant to say was:

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

-3 - (-2) = -1

NM

Re: two's compliment math

http://www.gamezero.com/team-0/articles/math_magic/micro/boolean.html

says it all i think