Shop OBEX P1 Docs P2 Docs Learn Events
2’s Complement — Parallax Forums

2’s Complement

Mike GMike G Posts: 2,702
edited 2008-01-13 19:49 in Propeller 1
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

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2008-01-13 19:18
    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)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-13 19:29
    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
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-13 19:39
    Phil, you might be right according to the book, but I did exactly what the OP requested smile.gif So let's call it 1's complement
  • Mike GMike G Posts: 2,702
    edited 2008-01-13 19:40
    Your right my mistake. I need 1's complement. Thanks guys
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-13 19:49
    Well, that'll teach me about reading the title and just skimming the original code! smile.gif

    -Phil
Sign In or Register to comment.