View Full Version : 2’s Complement
Mike G
01-14-2008, 02:11 AM
I’m sure there is a quicker way to calculate 2’s complement than my 3 lines of Propeller assembly code?
neg chkSum,t1
sub chkSum,#1
and chkSum,#$FF
deSilva
01-14-2008, 02:18 AM
Yes, it is generally done by
XOR chkSum, cTRUE
...
cTRUE LONG TRUE
However it seems you want the least 8 bits only, then it would be
XOR chkSum, #$FF
AND chkSum, #$FF
You will not need the AND if the original bits 8..31 are already cleared.
Phil Pilgrim (PhiPi)
01-14-2008, 02:29 AM
deSilva,
Your code does a 1's complement. For 2's complement, you need to add a ADD chkSum,#1 between the XOR and AND. But this would be quicker:
NEG chkSum, chkSum
AND chkSum, #$FF
-Phil
deSilva
01-14-2008, 02:39 AM
Phil, you might be right according to the book, but I did exactly what the OP requested http://forums.parallax.com/images/smilies/smile.gif So let's call it 1's complement
Mike G
01-14-2008, 02:40 AM
Your right my mistake. I need 1's complement. Thanks guys
Phil Pilgrim (PhiPi)
01-14-2008, 02:49 AM
Well, that'll teach me about reading the title and just skimming the original code! http://forums.parallax.com/images/smilies/smile.gif
-Phil