Shop OBEX P1 Docs P2 Docs Learn Events
(Urgent) How can I tell the basic stamp to define negative number?? — Parallax Forums

(Urgent) How can I tell the basic stamp to define negative number??

Leung Cheuk FungLeung Cheuk Fung Posts: 5
edited 2013-05-17 07:24 in BASIC Stamp
During receive data from sensor,I found that basic stamp can not define negatives and read as positive number. How can I modify the code to define negative number??

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-17 07:24
    The Basic Stamps use 16-bit 2's complement arithmetic. Bytes are considered unsigned positive values. If your sensor provides some other size number, say a 12-bit 2's complement value, you'll have to convert it like this:

    IF (value & $800) > 0 THEN value = value | $F000

    This checks the sign bit of the 12-bit value. If it's non-zero, the value is negative, so the sign bit is extended to make a 16-bit 2's complement negative value. If you sensor deals with some other kind of value, you'd have to do something appropriate for that.
Sign In or Register to comment.