Is Boolean FALSE?

New to FreeBASIC? Post your questions here.
MajorDill
Posts: 26
Joined: Sep 07, 2009 6:03
Location: Augusta, Kentucky

Is Boolean FALSE?

Postby MajorDill » Sep 07, 2009 6:22

I decided to start off my new membership with maybe the dumbest question ever asked.

Why does FB error me out when I try to:

Dim oRet as BOOLEAN

error 14: Expected identifier

I don't see BOOLEAN in the manual but FBedit seems to think it is a
keyword by offering it in big blue capital letters along with BOOL
Aave
Posts: 128
Joined: Jun 13, 2008 19:55
Location: Helsinki, Finland
Contact:

Postby Aave » Sep 07, 2009 6:57

FB doesn't (at least currently) have a boolean data type, but FbEdit also highlights also some additional things, like opengl commands and some WinAPI calls. TRUE and FALSE are defined in many include headers, but you can use e.g. the following:

Code: Select all

#Define FALSE 0
#Define TRUE (Not FALSE)

Declare your "boolean" variables as byte (for minimal memory usage, integer or short works also).
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Sep 07, 2009 11:13

Aave wrote:Declare your "boolean" variables as byte (for minimal memory usage, integer or short works also).

For 32-bit code, unless you are using a large number of them, boolean variables should probably be 32-bit integers.
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Sep 07, 2009 13:00

Also, FB evaluates conditional expressions as 0=false, else=true. Also the NOT operator only toggles (-1) so if TRUE=1 then NOT TRUE = -2, which still evaluates to a true condition. So either use TRUE=-1 and FALSE=0 or just don't try to use the NOT operator with conditions.

Aave's example is perfect.

-Vince
counting_pine
Site Admin
Posts: 6169
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Sep 07, 2009 13:42

I think FBedit also highlights the various types and functions from the Windows API (#include "windows.bi"), which include BOOL and BOOLEAN.

A simple way to get a Boolean type is to use an enum:

Code: Select all

Enum Boolean
    False = 0, True = Not False
End Enum

Dim a As Boolean = True
Print a
But bear in mind that a simple enum will have the same limitations that vdecampo talks about - e.g. if you set a equal to 1, then both 'a' and 'not a' will evaluate as true.
fantastic
Posts: 106
Joined: Aug 21, 2005 13:41

Postby fantastic » Sep 20, 2009 22:31

!!!!!! That explains why I had problems with my code recently. I gave up and went back to using integer flags. The code below highlights the problem. Isn't this a major trap for all programmers using boolean? I've now created another enum named TRUEFALSE to do it correctly and avoid conflicts with windows.bi's BOOLEAN and BOOL statements.

Code: Select all

#Include "Windows.bi"

Dim TrueFalse As Boolean = True

print "Should be True; is ";
if TrueFalse then print "True" else print "False"
TrueFalse = not TrueFalse
print "Should be False; is ";
if TrueFalse then print "True" else print "False"

sleep
jevans4949
Posts: 1148
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » Sep 20, 2009 22:51

@fantastic: As mentioned, FB doesn't have a built-in Boolean type. FB's behaviour (zero=false, non-zero=true, and returning -1 for a logical true) conforms with that of C when using numerical types. Most people understand that, I think.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Sep 20, 2009 23:59

C uses 1 for logical true, though.
fantastic
Posts: 106
Joined: Aug 21, 2005 13:41

Postby fantastic » Sep 21, 2009 2:16

jevans4949 wrote:@fantastic: As mentioned, FB doesn't have a built-in Boolean type. FB's behaviour (zero=false, non-zero=true, and returning -1 for a logical true) conforms with that of C when using numerical types. Most people understand that, I think.


Yes, I understand that. All I was saying is those using the windows include may as I did fall into the trap in assuming the enumerated boolean variables can be used like built-in boolean variables in other languages when clearly what I've found is they cannot, unless I'm thick and missing something. From my point of view as a multi-language programmer it's a very serious issue.
counting_pine
Site Admin
Posts: 6169
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Sep 21, 2009 3:25

I recommend you avoid using NOT unless you're absolutely sure you're working with a value that's either 0 or -1. If you want to check if something is false, compare it to FALSE. FALSE is always 0.
fantastic
Posts: 106
Joined: Aug 21, 2005 13:41

Postby fantastic » Sep 21, 2009 3:48

counting_pine wrote:I recommend you avoid using NOT unless you're absolutely sure you're working with a value that's either 0 or -1. If you want to check if something is false, compare it to FALSE. FALSE is always 0.


Is this a general tip or just specific to FB?
rdc
Posts: 1713
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Sep 21, 2009 10:58

fantastic wrote:
counting_pine wrote:I recommend you avoid using NOT unless you're absolutely sure you're working with a value that's either 0 or -1. If you want to check if something is false, compare it to FALSE. FALSE is always 0.


Is this a general tip or just specific to FB?


Primarily just FB, although VB 6.0 followed this practice. Each language implements this differently and it really depends on if there is a built-in boolean type, and how logical operators are implemented; i.e., logical operations or bitwise operations. FB uses bitwise operations which is why Not (0) become -1.
fantastic
Posts: 106
Joined: Aug 21, 2005 13:41

Postby fantastic » Sep 21, 2009 11:48

Yes, it's all so unfortunate. Also, if you set a variable as boolean type True, not of that variable = 4294967294. If the variable is set to Boolean False, then not of that variable is 4294967295.

Just hope not too many programmers are wasting time on this. I almost did. I caught it just in time after realizing something was wrong when I converted a program in another language to FB. The following code yields these results with #windows.bi:

Boolean True should be True; is True
not boolean True should be False; is True
not true = false
not false = true
not false = not true

Sad really that we can't use these in "not" operations. Oh well, I'll just have to remember this annoying trap and use my own enumerations.


Code: Select all

#include once "windows.bi"

Dim TrueFalse As Boolean = True

Print "Boolean True should be True; is ";
If TrueFalse Then Print "True" Else Print "False"
TrueFalse = Not TrueFalse
Print "not boolean True should be False; is ";
If TrueFalse Then Print "True" Else Print "False"
if not true = false then print "not true = false" else print "not true <> false"
if not false = true then print "not false = true" else print "not false <> true"
if not false = not true then print "not false = not true" else print "not false <> not true"
sleep
counting_pine
Site Admin
Posts: 6169
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Sep 22, 2009 18:10

Just to clarify, my tip about avoiding NOT applies wherever NOT does an "XOR -1" instead of an "= 0". This includes most BASICs, I think.

@fantastic, your code falls foul of operator precedence assumptions. (NOT a = b) is equivalent to (a <> b). You should wrap the intended NOT operation in parentheses.
The PRINT statements should probably be similarly adjusted.

Oh, and it should really go without saying, but because of the different possible TRUE values, a value should never be compared to TRUE, unless the possible TRUE value for it is very well defined.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » Sep 22, 2009 18:45

Here's something I whipped together in 5 minutes.. put this in a file called "boolean.bi" and #include it to use this. (you should separate the declarations from the code bodies if you use this with modularized code)

Code: Select all

type boolean
    private:
    m_value as integer
   
    public:
    declare constructor
    declare constructor(byref as boolean)
    declare constructor(byval as integer)
    declare operator cast as integer
    declare operator cast as string
    declare operator let (byval as integer)
end type

declare operator = (byval as boolean, byval as boolean) as boolean
declare operator and (byval as boolean, byval as boolean) as boolean
declare operator or (byval as boolean, byval as boolean) as boolean
declare operator xor (byval as boolean, byval as boolean) as boolean
declare operator not(byval lhs as boolean) as boolean

static as boolean true = -1
static as boolean false = 0

constructor boolean()
    m_value = 0
end constructor

constructor boolean(byref src as boolean)
    this.m_value = src.m_value
end constructor

constructor boolean(byval src as integer)
    m_value = iif(src <> 0, -1, 0)
end constructor

operator boolean.cast as Integer
    return (m_value <> 0)
end operator

operator boolean.cast as string
    return *iif(m_value, @"True", @"False")
end operator

operator boolean.let(byval rhs as integer)
    this.m_value = (rhs <> 0)
end operator

operator and (byval lhs as boolean, byval rhs as boolean) as boolean
    return boolean((lhs <> 0) and (rhs <> 0))
end operator

operator or (byval lhs as boolean, byval rhs as boolean) as boolean
    return boolean((lhs <> 0) or (rhs <> 0))
end operator

operator xor (byval lhs as boolean, byval rhs as boolean) as boolean
    return boolean((lhs <> 0) xor (rhs <> 0))
end operator

operator = (byval lhs as boolean, byval rhs as boolean) as boolean
    return ((lhs <> 0) = (rhs <> 0))
end operator

operator not (byval lhs as boolean) as boolean
    return boolean(not(lhs <> 0))
end operator

 

''' test code

print (not false xor true) and false 'false
print true xor false                 'true
print false = false

dim t as boolean

for i as Integer = 0 to 10
    print t
    t = not t
next

sleep
Last edited by voodooattack on Sep 22, 2009 23:18, edited 2 times in total.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 4 guests