Menu

Integers: signed math operations in GCBasic.

Santiago
2009-03-25
2013-05-30
  • Santiago

    Santiago - 2009-03-25

    Now that i learned a little bit more about math operation with byte and word variables, i think that i understand how to do this.

    First i have to say that the "signed_math" functions i posted are no needed, so i think that the posts about that should be deleted by some admin to aviod confusing people (Kent: if you can, do it please).

    The proper way to manage this is using word variables and testing the 7th bit of high byte, as Kent suggested (and i dind't understand in that moment). The only thing is that we have to be sure that the result will keep betwen -32767 and +32767.

    Or using Byte variables for values betwen -127 and +127.

    Then it can work this way:
    ________________________________________________

    dim first_var as word
    dim second_var  as word
    dim result as word

    first_var = 10
    second_var = -1200 'GCBasic accepts negative values

    result = first_var + second_var
    ________________________________________________

    'To have the absolute value of result:

    dim result_abs as word

    if result_H.7 on then 'if result is a negative value
    result_abs = 65535 - result + 1
    else
    result_abs = result
    _________________________________________________

    GCBasic can also accept "integer" variables, but i'm not sure how they work, perhaps someone that know it could tell us something about this.

     
    • Nobody/Anonymous

      Ok... it's easier than that.

      Just do this:

      'To have the absolute value of result:

      dim result_abs as word

      if result_H.7 on then 'if result is a negative value
      result_abs =  - result  
      else
      result_abs = result

       
    • Hugh Considine

      Hugh Considine - 2009-03-25

      Integer variables in GCBASIC are word variables that the compiler will treat in a special way. They're stored as 16 bit two's complement, which gives a range of -32768 to 32767. At present, there are routines for negate, multiply and divide on integers built in to the compiler. A nice feature of two's complement integers is that they can use the word routines for add, subtract, equal and not equal.

      There are still no integer operations for less than and less than or equal - the compiler is already altered, I just haven't yet gotten around to writing the SysCompLessThanInt orSysCompLessOrEqualInt routines in system.h. There may also be a few bugs, hardly any testing has been done on integers.

       
    • Santiago

      Santiago - 2009-03-25

      Hi Hugh... thanks for the explanation.

      Then if i undertand, integer variables are just word variables, but using the absolute value, and bit7 of high byte as sign??, something like this:
      ________________________________________________

      if result_H.7 on then 'if result is a negative value
      result = 65535 - result + 1 
      result_H.7 = 1
      else
      result = result
      _________________________________________________

      Anyway i know this is not a GCBasic specific cuestion... so i will look for information about this in the web.

      I'm happy to know that integers are near to be full supported by GCBasic. Let me know if you want me to do some specific testing.

      Greetings.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.