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


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

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

    But if ADC value gets below 150, then things get bad.
    what does get bad mean ?

    show your code

  2. #2
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

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

    Variable value should go to negative, but since we have no negative, it goes into 65xxx range

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

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

    Variable value should go to negative, but since we have no negative, it goes into 65xxx range
    only if you interpret it incorrectly and ignore the sign bit



    show your code
    Warning I'm not a teacher

  4. #4
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

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

    So by checking the last bit, I can know, whenever variable went into negative world, right?

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

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

    That is what post #3 says. Henrik explained it well.

    Ioannis

  6. #6
    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.

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


    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

Similar Threads

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

Members who have read this thread : 3

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