en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en en nl en en en en en it en en en en en en en en en en en de nl en en en en nl en en en en nl uk en en en en en en en en nl en en en en en en en fr en da en en en en en en en nl en en en tr tl fr en en en en en en en en en en en en en en en en nl en en en en en en it en en en en nl en en en en en en en en it en en en en en en en nl vi en en en en en en en en en en en en en en en nl en en en en en en en en pt en en en en fr en da en en ceb en en en en nl fr en en en en en en en en en en en ceb en kn en en en en en en en en de en en en fr en en en en en en en en en pt en en en en en en
By logging into your account, you agree to our Privacy Policy, personal data processing and storage practices as described therein.
Forgot password or user name?
Search in titles only Search in PowerBASIC for Windows only
You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
Long vs Dword Vs Boolean
All Time Today Last Week Last Month
All Discussions only Photos only Videos only Links only Polls only Events only
Since "Boolean" (at least in VB and other languages) values (True vs False) where:
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? "
Join Date: Mar 1999
4 Jan 2008, 05:06 PM
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)
*** Opinions expressed here are not necessarily untrue ***
Join Date: Aug 1998
4 Jan 2008, 05:47 PM
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.
Port Washington WI USA
Join Date: Aug 2006
4 Jan 2008, 06:14 PM
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 )
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? "
Join Date: Dec 2001
4 Jan 2008, 09:29 PM
you may have a look at Aldo's point of view.
Join Date: Mar 2004
4 Jan 2008, 09:58 PM
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).
#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
Join Date: Aug 2005
5 Jan 2008, 08:06 AM
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
Join Date: Aug 1998
5 Jan 2008, 08:41 AM
The simple answer is, if you are comparing two integers BY VALUE, the type specifiers must be consistent.. e.g....,
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...
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).
Port Washington WI USA
Join Date: Mar 2002
10 Jan 2008, 11:02 AM
"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  . 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)
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?
Copyright © 1998-2022 PowerBASIC Tools, LLC - All Rights Reserved.
Powered by vBulletin® Version 5.7.0
Copyright © 2022 vBulletin Solutions, Inc. All rights reserved.
All times are GMT-5. This page was generated at 05:15 AM.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.