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

Just starting out? Need help? Post your questions and find answers here.
Morlet Eric
New User
New User
Posts: 3
Joined: Mon Jul 28, 2008 1:33 pm
Location: Brussels

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

Post 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
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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 
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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)
oh... and have a nice day.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

@kaeru

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

Thank you !
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply