Announcement

Collapse
No announcement yet.

Long vs Dword Vs Boolean

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Long vs Dword Vs Boolean

    Since "Boolean" (at least in VB and other languages) values (True vs False) where:
    False = 0
    True = NON - Zero

    Excluding the ranges of DWORD vs Longs where:
    DWORD range of 0 to 4,294,967,295 ( 0 to 2^32-1)
    LONG range, from -2,147,483,648 to +2,147,483,647 ( -2^31 to 2^31 - 1)

    Is there an advantage or disadvantage of a variable being declared as DWORD or LONG? Besides personal preference (or a possible value returned that does not fall within the correct range for TRUE?)

    I keep catching myself porting VB to PB and sometimes I use LONG, sometimes I use DWORD for a "Boolean" reply depending on my mood, or thought process at the moment, or phase of the moon or something.

    Later it gets confusing because I wonder when I wrote it "Why did I declare it that way"? so I would like to break that habit.
    The only drawback would be if the variable were to hold the value of something I did not write (like an API, or someone elses dll) so then of course it would have to be of the same type as the type. but if it were my type, then it would be 0 or 1 so I am not worried about the actual values returned at the moment, but more advantages/disadvantages since both take 4 bytes, its the ranges that vary.
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    When TRUE is non-zero

    If TRUE is defined as non-zero, it might return a negative value. In that case you need to use a signed integer variables: LONG (or even QUAD, maybe?). Only if you are 100 procent sure that a boolean returns a positive integer value you may use WORD or DWORD.

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

    Comment


    • #3
      LONG is adantageous because the compiler is optimized for long.

      If it's Windows or any other external returning something, LONG/DWORD is immaterial... 32 bits are returned.

      Sign is immaterial, since booleans should only be tested with ISTRUE or ISFALSE.
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        I will have to check the ISTRUE ISFALSE statements, but for the moment assume the same meaning...aka
        ISFALSE = check if 0
        ISTRUE = check if NON-Zero

        ?

        and from there my checks are if the value of "TRUE" is within the DWORD range? or Within the LONG range, and try to not use a global that could get messed up with an invalid value depending on what I declared it as.

        A side note that brings a thought to mind (hey the synapse may not fire all the time, but when they do its a BIG BANG :laugh: )

        In VB before I really started getting into PB there was a subject I barely scratched the surface, but if possible could help fight off later bugs.
        In VB there was a function something like "IsObjectType" that I think I could use to tell me "How was variable declared?" sort of thing. So I just wondered if there was the same sort of thing in PB (or a API) or something, that I could add that extra level of (If LONG, then declare LONG, if DWORD, then Declare DWORD)

        Its off the wall, but I just had to ask in case it was possible? and I do not know it?
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          Cliff,
          you may have a look at Aldo's point of view.

          Comment


          • #6
            I think you're working too hard on a non-issue. Regarding logic operations both LONG and DWORD are 32 bits. The range is 32 bits. If none of the bits is set it is logical false. If any one, or more, of the bits is set it is logical true.

            In the code I set a long and a dword outside their numeric range. Of course for good programming practice you wouldn't do this on purpose. If you need a variable that is strickly bool, then make a personal rule to use either LONG or DWORD, and stick to it for all new coding you do. (I use LONG myself.) For numeric variables use LONG or DWORD as needed. You do need to be careful when comparing different types (">", "<", etc).
            Code:
            #compile exe
            #dim all
            
            function pbmain () as long
              local Lg as long
              local Dw as dword
              local Str1 as string
              local Str2 as string
              Lg = 4294967295
              Dw = -1
              if istrue Lg then
                Str1 = "True "
              else
                Str1 = "False "
              end if
              if istrue Dw then
                Str2 = "True "
              else
                Str2 = "False "
              end if
              msgbox "Long " + Str1 + bin$(Lg, 32) + $crlf + _
                     "Dword " + Str2 + bin$(Dw, 32) + $crlf
            end function
            added: outside numeric range, but within 32 bits
            Last edited by Dale Yarker; 4 Jan 2008, 10:10 PM. Reason: added caviat
            Dale

            Comment


            • #7
              Originally posted by Cliff Nichols View Post

              Is there an advantage or disadvantage of a variable being declared as DWORD or LONG? Besides personal preference (or a possible value returned that does not fall within the correct range for TRUE?)

              I keep catching myself porting VB to PB and sometimes I use LONG, sometimes I use DWORD for a "Boolean" reply depending on my mood, or thought process at the moment, or phase of the moon or something.
              You can define a macro

              MACRO boolean = LONG ' OR use DWORD if you prefer

              Then define your variables as

              Dim myboolean as boolean

              Comment


              • #8
                The simple answer is, if you are comparing two integers BY VALUE, the type specifiers must be consistent.. e.g....,
                Code:
                  hFile = CreateFile (...)
                  IF hFile = %INVALID_HANDLE_VALUE THEN  ' or > or < 
                       ...
                .. now 'hFile' and the value %INVALID_HANDLE_VALUE must be consistent. (In this case, the DECLARE'd return type of CreateFile is immaterial, since either DWORD or LONG will get back all 32 bits).

                But if you are doing a boolean check...
                Code:
                  iRet = SendMessage (....) 
                  IF ISTRUE iRet THEN 
                   ....
                .. now iRet and the return type of SendMessage must simply be in the same class, in this case, 32-bit integers. (In practice, iRet could be a 16-bit integer, because the compiler 'covers' for you. But I'd stick with a 32-bit integer).


                MCM
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Boolean Variables

                  "A Boolean variable can be used to hold the integer values 0 or 1, or the C++ literals true and false, which are implicitly promoted to the integers 0 and 1 whenever an arithmetic value is necessary. The type specifier to declare a Boolean variable is bool in C++. To declare a Boolean variable in C, use the bool macro, which is defined in the header file <stdbool.h>. A Boolean variable may not be further qualified by the specifiers signed, unsigned, short, or long.

                  The Boolean type is unsigned and has the lowest ranking in its category of standard unsigned integer types. In simple assignments, if the left operand is a Boolean type, then the right operand must be either an arithmetic type or a pointer. An object declared as a Boolean type uses 1 byte of storage space, which is large enough to hold the values 0 or 1.

                  In C, a Boolean type can be used as a bit field type. If a nonzero-width bit field of Boolean type holds the value 0 or 1, then the value of the bit-field compares equal to 0 or 1, respectively.

                  The token bool is recognized as a keyword only when used in a vector declaration context and when the AltiVec language extensions have been enabled.

                  _Bool uses 4 bytes of storage space, and is 4-byte aligned.

                  Variables of type bool can hold either one of two values: true or false. An rvalue of type bool can be promoted to an integral type. A bool rvalue of false is promoted to the value 0, and a bool rvalue of true is promoted to the value 1.

                  The result of the equality, relational, and logical operators is of type bool: either of the Boolean constants true or false.

                  Use the type specifier bool and the literals true and false to make boolean logic tests. A boolean logic test is used to express the results of a logical operation. For example:

                  bool f(int a, int b)
                  {
                  return a==b;
                  }


                  If a and b have the same value, f() returns true. If not, f() returns false."

                  Michael is right. Stay with a 32-bit integer preferably a LONG.

                  I would have to ask why do you define this all the time? Why not have an addon header file with this var defined or macroed?

                  Cheers

                  Comment

                  Working...
                  X