Shop OBEX P1 Docs P2 Docs Learn Events
Negative arithmetic — Parallax Forums

Negative arithmetic

DlwigwamDlwigwam Posts: 6
edited 2013-05-13 20:25 in BASIC Stamp
I know that negative numbers cut the range of a variable, but is there a special way to do arithmetic? The following gives me an overflow value, rather than -30:

DEBUG DEC 20-50

Comments

  • ElectrodudeElectrodude Posts: 1,648
    edited 2013-05-13 12:54
    It gave you 65506, right? Try this:
    DEBUG SDEC 20-50     'should print 30
    
    The SDEC tells the DEBUG command that your number is signed (-32768 - +32767) as opposed to unsigned (0-65535). 65506=65536-30
  • DlwigwamDlwigwam Posts: 6
    edited 2013-05-13 19:21
    Thanks! So the value is stored as negative, and we just need to let DEBUG know that?
  • SRLMSRLM Posts: 5,045
    edited 2013-05-13 20:25
    It all depends on how you interpret the number. The S in SDEC tells DEBUG to interpret the number as 2's complement.
Sign In or Register to comment.