Logical operators

Archives Forums/BlitzMax Bug Reports/Logical operators

hamZta(Posted 2008) [#1]
Hey guys!

I'm not quite sure but I think logical operators with strings behave a bit strange:

Local str1:String = "a"
Local str2:String = "defgh"

Print (str1 And str2)


This piece prints "5", I expected "1" (True).
Apparently, the logical operation always returns the length of the second operand.
Using OR, it's the other way round, it returns the length of the first string.

Print ("a" And "defgh")

This does what I expected: Returns 1 (True)

As you can see, putting the operands in variables first alters the result of the logical operation.

Greetings,
hamZta


TaskMaster(Posted 2008) [#2]
I don't think you can even AND two strings together and you are just getting completely random results. What were you expecting to accomplish doing this?


Yan(Posted 2008) [#3]
@TaskMaster - That's perfectly valid, remember that in BMax, And is logical and & is boolean.


This is just the way the logical operators have always worked in BMax. This may well be a bug, but I've always assumed it was a speed thing.

It works the same way for numbers too...
Local str1:String = "a"
Local str2:String = "defgh"
Local str3:String

If (str1 And str2) Then Print "yes" Else Print "no"
If (str3 And str1) Then Print "yes" Else Print "no"

Print (5 And 10)
...anything other than 0 is considered true with comparisons so not really a problem.