|
<<
Index
>>
|
FutureBasic
|
or - bitwise 'or'
|
|
operator
|
|
Syntax
result = exprA or exprB
result = exprA | exprB // the vertical bar is shorthand for 'or'
Description
Expression exprA and expression exprB are each interpreted as integer quantities.
The or operator performs a "bitwise comparison" of each bit in exprA with the corresponding bit in exprB. Meaning: bit 0 in exprA is compared to bit 0 in exprB,
bit 1 in exprA is compared to bit 1 in exprB and so forth for all the bits.
The result is another integer; each bit in the result is determined as follows:
Bit Value in exprA |
Bit Value in exprB |
Bit Value in result |
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
Examples
The example below shows how bits are manipulated with or
defstr long
print bin$(923)
print bin$(123)
print "--------------------------------"
print bin$(923 or 123)
program output:
00000000000000000000001110011011
00000000000000000000000001111011
--------------------------------
00000000000000000000001111111011
Caution!
If your code needs to evaluate true/false conditions based on variable content, a logical 'or' using double vertical bars ('||
' )
is more reliable because bitwise comparisons can provide different/incorrect comparison results from their logical peers.
Expressed differently, if you're NOT doing bit manipulations/testing, the logical or is most-likely what is needed.
See logical or here: logical '||'
The next example demonstrates bitwise ORing producing an incorrect result when logical ORing works
short a,b : a = 1 : b = 2
defstr byte
print"Decimal",,"Binary"
print a,,, bin( a )
print b,,, bin( b )
print ,,,"--------"
print ,,,bin$( a and b ), "bitwise ANDing results in all binary zeros which might be unexpected"
print
print "Bitwise ANDing produces: ",( a and b ), "incorrect if logical result was expected"
print "Logical ANDing produces: ",( a && b ), "i.e. true"
print "Bitwise ORing produces: ", ( a or b ), "could accidentally work but not what the programmer usually wants"
print "Logical ORing produces: ", ( a || b ), "i.e. true"
program output:
Decimal
Binary
100000001
200000010
--------
00000000
bitwise ANDing results in all binary zeros which might be unexpected
Bitwise ANDing produces: 0
incorrect if logical result was expected
Logical ANDing produces: 1
i.e. true
Bitwise ORing produces: 3
could accidentally work but not what the programmer usually wants
Logical ORing produces: 1
i.e. true
See also
|| - logical 'or';
&& - logical 'and';
not;
! - logical 'not';
and - bitwise;
xor;
Appendix D - Numeric Expressions