Shop OBEX P1 Docs P2 Docs Learn Events
Whats with (no) negative numbers in the BS1? — Parallax Forums

Whats with (no) negative numbers in the BS1?

AmnoidAmnoid Posts: 11
edited 2005-02-24 22:09 in BASIC Stamp
I did some basic math to compare two analog inputs on a BS1. The manual sais that I can see negative nubers (i.e. 10- 20= -10). But instead I get something near 65533 (similar to when an 8bit variable reaches the end of its number stack·and starts back at 0).... I am using word variables (W0 - W1), but still don't see any NEGATIVE numbers. I was able to work around this, but I would still like to use simple math. Any hints?

Thanks

Comments

  • Robert SchwartzRobert Schwartz Posts: 141
    edited 2005-02-24 00:15
    The Basic Stamps used unsigned numbers. The way this works is that signed numbers have an extra bit in them that determines if they arepositive or negative. Parallax figured that more people would want to use the larger numbers than negative numbers, so they made all of the data types unsigned, meaning that they use that bit to store larger numbers.
  • AmnoidAmnoid Posts: 11
    edited 2005-02-24 06:24
    Here is what it sais in the BS help file:

    - Subtraction
    The Subtraction operator (-) subtracts variables and/or constants, returning a 16-bit result. Works exactly as you would expect with unsigned integers from 0 to 65535. >>>>> If the result is negative, it will be correctly expressed as a signed 16-bit number. <<<<<

    So where is the mistake? Is there a way to change a number to signed?
  • BushmanBushman Posts: 4
    edited 2005-02-24 06:45
    You can use ABS to get the absolute difference between the two.

    compare VAR Word
    value1 VAR Word
    Value2 VAR Word


    compare = ABS (value1 - value2)


    Though im not sure if this helps you or not sorry
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-02-24 16:12
    All BASIC Stamps store negative values in two's compliment format -- this is required in an integer only system. After a subtraction you can look at bit15 of the result; it that bit is set the value is negative.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • AmnoidAmnoid Posts: 11
    edited 2005-02-24 22:09
    Thanks for the tips. I solved it by checking the distance between 0 and the value- positive numbers is obvious, "negative" numbers simply count down from the last possible number in the 16bit scale. Weird- but it works.

    Thanks
Sign In or Register to comment.