BB___Logical_Operators

=Boolean (logical) operators= toc

Description
//Boolean (logical)// expressions **must** produce a result that is either True (nonzero) or False (0). Logical refers to ways of combining two or more values to produce a True or False result.

The JB logical operators list: > AND, OR, XOR and the NOT function.

General syntax
> //boolean_expression// operator //boolean_expression// Result is a boolean expression.

Truth tables
Here is how logical operators are defined ("truth tables"):
 * = **X** ||  ||= **NOT(X)** ||
 * = TRUE ||  ||= FALSE ||
 * = FALSE ||  ||= TRUE ||


 * = **X** ||= **Y** ||  ||= **X AND Y** ||   ||= **X OR Y** ||   ||= **X XOR Y** ||
 * = FALSE ||= FALSE ||  ||= FALSE ||   ||= FALSE ||   ||= FALSE ||
 * = FALSE ||= TRUE ||  ||= FALSE ||   ||= TRUE ||   ||= TRUE ||
 * = TRUE ||= FALSE ||  ||= FALSE ||   ||= TRUE ||   ||= TRUE ||
 * = TRUE ||= TRUE ||  ||= TRUE ||   ||= TRUE ||   ||= FALSE ||

Hints
> Usually logical operators are used to combine conditions in conditional statements or loops.

> A //boolean_expression// is usually used by Relational operators. With that, you are safe, because they return strictly 0 or 1. But if you use numbers instead of //boolean_expression//, you may get unexpected results due to the fact that these operators actually are Bitwise operators. For example, 2 is not 0 and 1 is not 0, both seem to be TRUE, but 1 AND 2 produces 0 (FALSE).

> The function NOT returns TRUE as -1 for FALSE (0). If you use XOR, with one operand negated and second relational operator, then TRUE XOR TRUE will not return FALSE as it should (again, because of the Bitwise operators)

> **It looks like operator precedence not implemented - use parentheses instead.**

Example
Using NOT: code format="lb" 'EOF means End Of File. So this piece reads file #1, while end of file NOT reached. while not(eof(#1)) INPUT #1, txt$ print "INPUT item is: ";txt$ wend code Using AND, OR, XOR: code format="lb" 'to check if both conditions hold, you use AND if abs(x-x0)<10 and abs(y-y0)<10 then 'if we got close enough to x0, y0 by x and y coordinates, count a hit goto [hit] end if

'to check if at least one of conditions hold, you use OR if x 400 then 'if we got out of game field, change the velocity sign to go back dx=-1*dx end if

'to check if exactly ONE of two conditions hold, you use XOR 'now if someone has an example with some meaning, please post code

Useful Procedures
code format="lb" 'usage sample: 'print x, "is between 2 and 8", between(x, 2, 8) 'between inclusive function between(x, min, max) between = (x>=min) and (x<=max) end function code  =Boolean (bitwise) operators=

Actually, JB logical operators are bitwise operators: > AND, OR and XOR It looks like they operate on 32-bit integers. > There is no bitwise NOT, but you can compute it as >> x XOR -1 > (-1 has all 32 bits set to 1, see results of BitPattern$ under Useful Procedures)

Hints
> Using logical operators on floating-point numbers makes no sense. > Sometimes, it will fail with a "IsEmpty not understood" error. > **(Do NOT expect JB to convert numbers to integers for you in this case.)**

> Using logical operators on big negative numbers sometimes will fail with a "IsEmpty not understood" error too.

> **It looks like operator precedence not implemented - use parentheses instead.**

Example
code format="lb" print "Example of bitwise operations usage" print "we will work with 8 bit, for simplicity." print "You can sure use up to 32 bit in JB." print

print "Let's start from 0" a=0 print tab(5);eightBitPattern$(a) print

print "To set some bits, we use OR" op = a op1$ = "1010" op1 = bin2num(op1$) print tab(5);eightBitPattern$(op) print " OR ";eightBitPattern$(op1) print tab(5);"" res = op or op1 print tab(5);eightBitPattern$(res) print

print "Now, add some more bits" op = res op1$ = "11" op1 = bin2num(op1$) print tab(5);eightBitPattern$(op) print " OR ";eightBitPattern$(op1) print tab(5);"" res = op or op1 print tab(5);eightBitPattern$(res) print "note last bit got set, but one just before doesn't change (stays set)" print

print "To mask (reset) some bits, we use AND" print "second operator commonly called a mask" print "bits where mask has 0 will get reset (masked)" op = res mask$="11111001" op1 = bin2num(mask$) print tab(5);eightBitPattern$(op) print "AND ";eightBitPattern$(op1); "<< the mask" print tab(5);"" res = op and op1 print tab(5);eightBitPattern$(res) print

print "To toggle some bits, we use XOR" print "let's toggle first and last bit" op = res toggeMask$="10000001" op1 = bin2num(toggeMask$) print tab(5);eightBitPattern$(op) print "XOR ";eightBitPattern$(op1) print tab(5);"" res = op xor op1 print tab(5);eightBitPattern$(res) print print "Second XOR with same number restores original" op = res print tab(5);eightBitPattern$(op) print "XOR ";eightBitPattern$(op1) print tab(5);"" res = op xor op1 print tab(5);eightBitPattern$(res) print

print "Last, XOR with all the ones works as bitwise NOT" op = res toggeMask$="11111111" op1 = bin2num(toggeMask$) print tab(5);eightBitPattern$(op) print "XOR ";eightBitPattern$(op1) print tab(5);"" res = op xor op1 print tab(5);eightBitPattern$(res) print

end

'- function bin2num(bin$) bin2num = 0 for i = 1 to len(bin$) c$=mid$(bin$, i, 1) if instr("01",c$)=0 then exit function 'like VAL, break on first non-valid digit bin2num = bin2num*2+(instr("01",c$)-1) next end function

function eightBitPattern$(num) for i = 0 to 7 eightBitPattern$ = str$((num and 2 ^ i) > 0) + eightBitPattern$ next i end function code

Useful Procedures
code format="lb" function BitPattern$(num) for i = 0 to 31 if i mod 8 = 0 then BitPattern$ = " " + BitPattern$ BitPattern$ = str$((num and 2 ^ i) > 0) + BitPattern$ next i end function code code format="lb" function bitwiseNot(num) bitwiseNot = num XOR -1 end function code