Shop OBEX P1 Docs P2 Docs Learn Events
Negative Numbers on BS2 — Parallax Forums

Negative Numbers on BS2

mountaineermountaineer Posts: 27
edited 2006-03-16 08:39 in BASIC Stamp
I am reading a value of byte variable (0-255) in psxJoyY, subtracting it from 127(to get the range -128 to 127) and storing that number in another byte, speed. I tested it with this code:

speed = 127-psxJoyY
DEBUG SDEC3 (127-psxJoyY), CR, SDEC3 speed

The first line of the debug(127-psxJoyY) has a range of -128 to 127. However, speed only appears positive. While increaseing psxJoyY from 0 to 255, the result should go down. However, it goes down from 127 to 0, then counts up to 128. Apparently, the bit that indicates a negative sign does not get stored in the byte speed.

Is there a way to fix this?

Thanks in Advance, botguy

PS. Can someone explain how the BS2 stores negative numbers? (bit wise)

PSS. I would like to say that I am very disappointed by the Basic Stamp 2 as a programmer. All negative variables are implemented poorly and not explained in the help file. All the user can do is add and subtract negitive number. Multiplying, dividing, and other operators are out of the question, along with compareing them. I spent a whole day debugging my code before I figured this out.

Comments

  • Russ MillerRuss Miller Posts: 25
    edited 2006-03-16 05:29
    Hi Mountaineer,

    The Basic Stamp manual describes this behavior in more detail than the help file on pg 103-107. http://www.parallax.com/dl/docs/prod/stamps/web-BSM-v2.2.pdf

    Basically, all operations are done two's complement on 16 bit numbers. When smaller variables (bit, nib, byte) are retreived the sign is never extended and when the result is stored in one of these smaller size variables the higher bits are truncated. If you know that a variable may be negative you can add extra code to take care of the sign.

    While handling this manually (or finding a different way to scale or offset your data) can be a pain the BS2 at least is consistant - I've chased quirky results on several (microcontroller) C compilers caused by unexpected type conversion behavior within expressions that I could only fix by using way more explict cast operators than should be required. I think it goes with microcontroller territory where we almost always use small, unusual storage types that are uncommon in workstations.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-03-16 08:39
    BasicStamp uses Twos Compliment and only that. 16 bit numbers only.
    You might try writing your numbers as 16bit values in HEX or Binary. And be careful to store as Words.

    There are several other systems of negative numbers that are explained in 'The Art of Electronics'.
    If you are confusing systems, you might read up on all of them in that text.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
Sign In or Register to comment.