How to detect variable going below zero, without using PBPL ?


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    Whether signed or unsigned, 0 = 0.
    With an Unsigned BYTE (let's start simple), the range is 0 to 255. From 255, add 1 and you get 0 again, as the BYTE rolls over.
    With a Signed BYTE, the range is -128 to +127. Think in terms of binary: 127d = %0111 1111. Make sense so far?

    Working with a Signed BYTE, if you calculate: "0 - 1 =", you get %1111 1111 which equals -1.
    With an Unsigned BYTE, if you calculate: "0 - 1 =" you still get %1111 1111, but it equals 255.

    Think about this for Signed Variables:
    0d = %0000 0000
    1d = %0000 0001
    -1d = %1111 1111
    -2d = %1111 1110

    Sometimes just looking at the representations enables the light bulb to turn on.

    Now if we're working at the 16-bit WORD level:
    0d = %0000 0000 0000 0000
    1d = %0000 0000 0000 0001
    -1d = %1111 1111 1111 1111
    -2d = %1111 1111 1111 1110

    See the trend?? I hope this helps you figure some things out.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,690


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    its called two's compliment , no magic involved

    https://en.wikipedia.org/wiki/Two%27s_complement
    Warning I'm not a teacher

  3. #3
    Join Date
    Feb 2013
    Posts
    1,154


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    I came into another issue.
    I have font array in EEPROM, and need to set offset for reading it. Depending on charset, this offset can be negative and positive.

    So I have to do it in a way like this:

    if langvar[x]=0 then
    Y=(topline[x])*8+OFS

    if langvar[x]=1 then
    Y=(topline[x])*8-OFS

    Can these two joined into single operation somehow?

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    Code:
    if langvar[x]=0 then
        Y=(topline[x])*8+OFS
    else
        Y=(topline[x])*8-OFS
    endif
    Ioannis

  5. #5
    Join Date
    Feb 2013
    Posts
    1,154


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    No, I meant changing the value of OFS in the way, that addition will cause actual subtraction, due to overflow.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    As long as both variables are of the same size adding a two's compliment value to another is the same as subtracting the absolute value of one from the absolute value of the other.

    In these examples all variables are bytes.

    X = 0
    OFS = 1
    Y = X + OFS Y will have the value of 1.
    ***********************************************
    X = 0
    OFS = -1 OFS will have the value 255
    Y = X + OFS Y will contain the value 255 (which is -1)
    ***********************************************
    X = 10
    OFS = -5 OFS will have the value 251
    Y = X + OFS Y will have the value value 5
    ***********************************************
    X = -10 X will have the value 246
    OFS = - 15 OFS wil have the value 241
    Y = X + OFS Y will have the value 231 which is the same as -25
    ***********************************************

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,690


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    in simple terms adding the two's compliment of a var to a number is equivalent to subtracting the var from a number
    providing they are all the same integer var type
    Warning I'm not a teacher

Similar Threads

  1. How to detect variable decrease moment?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th February 2014, 23:02
  2. PBPL and TCP/IP
    By edtp in forum Ethernet
    Replies: 10
    Last Post: - 4th March 2011, 20:35
  3. Help, PBPL and DIV 32.
    By Rogerio in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 16th March 2010, 11:37
  4. PBP and PBPL
    By keymuu in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 30th January 2009, 18:58
  5. IF..AND/OR..THEN and PBPL
    By duncan303 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th January 2008, 17:45

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts