Blitz3D+ Command Reference

expression1 And expression2

Parameters

expression1 - the value or condition on the left

expression2 - the value or condition on the right

Description

Combines two values bit by bit, keeping only the bits that are set in both.

And has two jobs in Blitz3D+, and they are the same operation underneath.

Most of the time you use it to join conditions: If lives>0 And shield>0 Then ... . That works because a comparison produces 1 for true and 0 for false, and 1 And 1 is 1 while anything And 0 is 0. And sits at the very bottom of the precedence table, below the comparison operators, so the comparisons on either side are worked out first and you rarely need brackets.

Its other job is masking bits. flags And 4 is non-zero only when bit 2 of flags is set, which is how you test one entry in a packed set of switches - which doors are open, which tutorial steps are done, which sides of a tile are solid.

The gotcha follows from the two jobs being one: And is bitwise, not logical shorthand. Two values that are both "true" in the loose sense can still come out false, because 1 And 2 is 0 - they share no bits. Only feed And real comparisons, or values you know are 0 and 1, and it will behave.

Note also that both sides are always evaluated; there is no short-circuiting, so a second test cannot rely on the first one having passed.

See also: Or, Not, Xor, If, Shl.

Example

; And Example
; -----------

; And is true only when BOTH sides are true - the everyday way to
; combine conditions in an If. On plain integers it also works
; bitwise, keeping only the bits set in both values.

keys=3
health=75

; Logical use: both conditions must hold
If keys>0 And health>50 Then
    Print "You have "+keys+" keys and "+health+" health - enter the dungeon!"
Else
    Print "You are not ready for the dungeon."
EndIf

Print ""

; Bitwise use: only bits set in BOTH values survive
a=%1100
b=%1010
Print "a       = "+Bin$(a)+" ("+a+")"
Print "b       = "+Bin$(b)+" ("+b+")"
Print "a And b = "+Bin$(a And b)+" ("+(a And b)+")"

Print ""

; Bitwise And tests flags: is bit 4 set in the combined value?
flags=1 Or 4
If flags And 4 Then Print "Flag 4 is set in flags."

Print ""
Print "Press any key to close the example"
WaitKey

End

Index