two's compliment math

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

Translate This Thread From English to

Threaded View


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


Quoted text here. Click to load it

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




Re: two's compliment math



Quoted text here. Click to load it
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



Re: two's compliment math


Quoted text here. Click to load it

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


Quoted text here. Click to load it

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


Quoted text here. Click to load it

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



Site Timeline