two's compliment math

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
Reply to
JJ
Loading thread data ...

dont you just add 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

Reply to
david

but

Yes, you negate one operand and add i.e. invert all bits, add 1 and then add 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

Reply to
JJ

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

David

Reply to
david

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

Reply to
News 2 Me

What I meant to say was:

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

-3 - (-2) = -1

NM

Reply to
News 2 Me

formatting link

says it all i think

Reply to
david

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.