Long Variables can be negative?


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322

    Default Long Variables can be negative?

    On page 15 of the PBP3 Reference Manual (1.3) it says that "Long variables are 32-bits wide and can hold values -2147483648 to 2147483648. Is it really true you can have a negative number? Thanks, Ed

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    Only if you use PBPL instead of PBPW then yes. You can make e.g. comparisons for negative numbers.

    Code:
    Bval VAR BYTE
    Bval = 0
    IF ((Bval – 1) < 0) THEN negative
    In the abobe example although Bval is byte, internally the PBPL holds the temporary result in a long variable which in turn can hold negative numbers too.

    So the result of the IF-THEN will goto negative.

    Ioannis

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    Ioannis,
    In the abobe example although Bval is byte, internally the PBPL holds the temporary result in a long variable which in turn can hold negative numbers too.
    So the result of the IF-THEN will goto negative.
    Are you sure about that?
    I thought that although you could "manually" look at the highest bit (IF BVal.7 THEN Negative) to see if it's negative doing a comparison for less than zero only really works with variables declared as LONGs. Or are you saying that it'll actually work because you're doing an intermediate calculation in your actual comparison ie
    Code:
    BVal VAR BYTE
    BVal = 0
    If (BVal - 1) < 0 THEN Negative
    Works, while...
    Code:
    BVal VAR BYTE
    BVal = 0
    BVal = BVal - 1
    If BVal < 0 THEN Negative
    ..doesn't?

    Guess I could try it myself....

    /Henrik.

  5. #5
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    First code will work, if you use PBPL, because result is stored into temporally long variable.
    That is same as this

    Temp VAR LONG
    BVal VAR BYTE
    BVal = 0
    Temp = BVal - 1
    If Temp < 0 THEN Negative

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


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    That's what I thought, makes sense, thanks!
    I haven't yet used PBPL in any project. I tried on my servo project but the "cost" of compiling with PBPL was too high so I managed without it.

    /Henrik.

  7. #7
    Join Date
    Jan 2005
    Location
    France
    Posts
    97


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    hello
    As am not super fluent in English, I understand that if I want to use PBPL.exe I need to use a 18Fxxx PIC
    I need to use a variable that will contents the number 39000000 (39 millions)
    it is use in a frequency generator that goes to this frequency
    my board (display and push buttons is equiped with a 16F877 so I have no chance to use such LONG variable !
    Am I right ?
    francois F1CHF
    thanks
    Last edited by F1CHF; - 26th December 2012 at 18:20. Reason: keyboard error !

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    Hi,
    Yes, LONGs can only be used on 18-series parts. Do you need to do any math etc on that 32bit variable or are you just trying to store it? If you only need to store it then simply use an array of 4 bytes. If you need math you could always take a look at Darrels N-Bit math routines which will allow you to declare variables of "any" size and do math with them.

    /Henrik.

  9. #9
    Join Date
    Jan 2005
    Location
    France
    Posts
    97


    Did you find this post helpful? Yes | No

    Default Re: Long Variables can be negative?

    Thanks Henrik for this quick answer
    But it is "too much" for my old brain !
    Darrel has done a great job , as usual !
    yes I have just one calculation (I got the Arduino code)
    something like that
    unsigned long tuning_word = (frequency * pow(2, 32)) / DDS_REF
    frequency will go from 1 to 39 millions
    DDS_REF is 125 millions
    and I must decode what is the POW(2,32) !
    For this need I think that I will order an Arduino board !

    Happy new year for everybody
    and all my congratulations for this very nice help forum, it is great
    Francois F1CHF (radio amateur) 65 years old now ....

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