Shop OBEX P1 Docs P2 Docs Learn Events
dealing with numbers that go negative (two's compliment) — Parallax Forums

dealing with numbers that go negative (two's compliment)

James LongJames Long Posts: 1,181
edited 2008-02-28 01:05 in Propeller 1
OK....I basically stick with Spin....because I just don't have time to learn PASM.

I've hit a problem, that until this point, I have not encountered.

A number that can go negative, and when it does, it is in two's compliment.

How does a programmer let the chip know if the items goes to two's compliment?

I'm sort of confused.....as how to prevent the program from thinking the number just jumped to some huge unreasonable number.

I'm·a little rusty with respect to programming....so be easy on me.

James L

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L

Partner/Designer
Lil Brother LLC (SMT Assembly Services)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-27 04:17
    Spin does all arithmetic in 32 bit signed arithmetic. All of the operators understand about negative numbers, so you don't have to tell the chip about anything. For a lot of the operators, the sign doesn't matter anyway like logical shifts or Boolean operations. Multiplication and division and the compare operators are signed operations. Things like addition and subtraction work the same whether signed or unsigned.
  • James LongJames Long Posts: 1,181
    edited 2008-02-27 04:25
    Mike Green said...
    Spin does all arithmetic in 32 bit signed arithmetic. All of the operators understand about negative numbers, so you don't have to tell the chip about anything. For a lot of the operators, the sign doesn't matter anyway like logical shifts or Boolean operations. Multiplication and division and the compare operators are signed operations. Things like addition and subtraction work the same whether signed or unsigned.
    Mike,

    Ok.....I see....so my little testing (Hyperterminal) is just a formating problem for the output for me.

    Thanks for the reply......I didn't actually think about it that way.....but that makes sense.

    I will have to put some led output and test the program to make sure the limit's are working right. I could go back and try to format the output....but it is not that important.

    James L

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L

    Partner/Designer
    Lil Brother LLC (SMT Assembly Services)
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-02-27 04:35
    For data sizes less than 32 bits you can use the sign extend ~X for 8bit arithmetic and ~~X for 16 bit arithmetic to maintain proper execution on a 32 bit syatem.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2008-02-27 20:30
    Ok

    So I have the numbers shown on Hyperterminal.....but I would like to format them to be signed.

    What is the best way to do this?· Just use the "~~" (the numbers are 16 bit)· ?

    It is important, because these number will at some point be stored in a file, And I would like to get the formating issue worked out.

    I'm not performing any calculation with the numbers at this point.....just recieving them from an accelerometer, and displaying them.

    James L

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L

    Partner/Designer
    Lil Brother LLC (SMT Assembly Services)
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-02-28 00:30
    Yes if it's 16 bit math, sign extend then send it via the DEC method, it will detect a negative and output the correct value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2008-02-28 01:02
    Paul,

    Ok....I'll modify my code....and see if I can get it to work out. If not...i'll be posting code here...to see if you guys can help out.

    I've been spending too much time on the SMT business....and not on my projects. Oh well.....as life goes.



    James L

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L

    Partner/Designer
    Lil Brother LLC (SMT Assembly Services)
  • RaymanRayman Posts: 14,162
    edited 2008-02-28 01:05
    This is another area that is easy to miss in the manual and could be handled better by a compiler with a more C-like syntax (like UINT and INT) declarations.

    As I understand it though:
    All variables not declared as bytes or words are longs. All longs are signed.
    If you RDbyte or RDword the source is treated as unsigned and the result is always positive.
Sign In or Register to comment.