Page 1 of 1

How does Purebasic handle 'true false' as well as &

Posted: Mon Jul 28, 2008 2:13 pm
by Morlet Eric
Look at the simple program below and to Debug's Output .
Question : how should one code
If (i=0)&(j=0)
for this program to work ?

Thanks,
Eric Morlet

i.w
j.w
tf.w

i=0
j=1
tf=(i=j)

Debug tf

Debug (i <= j)

Debug (i >= j)

If (i=0)&(j=0)
Debug i
Debug j
Debug "i=0 And j=0"
EndIf

;=======================================
; Debug Output:
1
1
1
0
1
i=0 And j=0

Posted: Mon Jul 28, 2008 2:20 pm
by Fluid Byte
Please use code tags and indention, it's hard to read.

But what is your problem anyway? You can't just ask how modify some random line in the code if we don't now what you are trying to do.

The following is just a wild guess:

Code: Select all

i.w
j.w
tf.w

i=0
j=1
tf=(i=j)

Debug tf
Debug (i <= j)
Debug (i >= j)

If (i=0) And (j=0)
	Debug i
	Debug j
	Debug "i=0 And j=0"
EndIf 

Posted: Mon Jul 28, 2008 2:22 pm
by gnozal
From the manual :

& : Bitwise AND. You should be familiar with binary numbers when using this operator. The result of this operator will be the value of the expression on the LHS anded with the value of the expression on the RHS, bit for bit. The value of each bit is set according to the table below. Additionally, if the result of the operator is not used and there is a variable on the LHS, then the result will be stored directly in that variable. This operator cannot be used with strings.

Code: Select all

; Shown using binary numbers as it will be easier to see the result
a.w = %1000 & %0101 ; Result will be 0
b.w = %1100 & %1010 ; Result will be %1000
bits = a & b ; AND each bit of a and b and use result in equals operator
a & b ; AND each bit of a and b and store result directly in variable "a" 
And : Logical AND. Can be used to combine the logical true and false results of the comparison operators to give a result shown in the following table.

LHS | RHS | Result
-----------------------
false | false | false
false | true | false
true | false | false
true | true | true

Code: Select all

If i = 0 And j = 0
  Debug "Both i and j are equal to zero"
EndIf

Posted: Mon Jul 28, 2008 2:23 pm
by Kaeru Gaman
"&" is a bitwise operator, "And" is the logical operator.
(i=0), (j=0) resp. (i <= j) are boolean expressions and not supported in PureBasic.



if you want to check both variables for false, you can write
If i=0 And j=0
or even
If Not i And Not j


[edith]
little demonstration of the difference of "&" and "And":

Code: Select all

a = %1111001111001111
b = %1010101010101010

If a And b
  Debug "both variables are not false"
EndIf

Debug "now the bitwise and:"
Debug Bin(a & b)

Posted: Mon Jul 28, 2008 3:20 pm
by Ollivier
Hio man!

Welcome ! :D

Sorry, but, actually you can't use this syntax :

Code: Select all

Debug (b = c)
I had the same little problem here

If we want to write a complex operation only in one line, we must replace it by a little procedure, for example :

Code: Select all

Procedure Equal(LHS.L, RHS.L)

Protected Result.L

If LHS = RHS: Result = -1: Else: Result = 0: EndIf

ProcedureReturn Result

EndProcedure
And use it, like that:

Code: Select all

Debug Equal(a, b)
Caution /!\ Cause you must create one procedure per type of variable !
(For example, EqualF() for float, et caetera...)

Caution too, cause not having this allows you to use the two logic supports in the same program :

Code: Select all

Logic A : True = 1
Logic B : True = -1
It's a advantage !

Bye!

Posted: Mon Jul 28, 2008 5:56 pm
by Kaeru Gaman
there is a dirty workaround to provide boolean expressions:

connect the expression logically with a boolean value:

Code: Select all

a = 7
b = 5

Debug ( (a > b) Or #False )

Debug ( (a > b) And #True )
CAUTION:
this is just a dirty workaround, there is no guarantee it would work in future PB-Versions.

Posted: Tue Jul 29, 2008 6:13 pm
by Ollivier
@kaeru

It's dirty but shorter than my method : no procedure and no adaptation for each type of variable.

Thank you !

Posted: Wed Jul 30, 2008 1:15 am
by blueznl