Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

OR

Keyword Abbreviation Token (hex) Version(s) Classification
OR none B0 1.0 to 7.0 Operator (7)

  Syntax  
subject OR mask
 
Parameters Type Legal Value(s) Default Value Note(s)
subject Float or Integer
-32768 to +32767
mask Float or Integer  -32768 to +32767     
 
Returns Type Value(s) Note(s)
result Integer
-32768 to +32767
Will be 0 or -1 if both parameters are either 0 or -1
 
  Purpose  
Evaluate a bit-wise boolean operation.

 
  Remarks  
The OR operator is often used in logical expressions, and may be considered a logical operator.  However, this is not really the case and can have nasty consequences if you're not aware of how it actually functions.  First, if either parameter is a string, a TYPE MISMATCH ERROR is generated.  Next, the each parameter, if floating-point, is converted to an integer; if the result is not a legal value (see above) then ILLEGAL QUANTITY ERROR is generated.  Finally, each of the 16 corresponding bit pairs is evaluated and the corresponding bit in the result is assigned according to this truth table:
 
OR Mask
0 1
Subject 0 0 1 Result
1 1 1
 
 
For bit-wise operations, it may be helpful to think of the mask as filtering the subject, such that if a mask bit is 0, the result bit is the same as the original subject.  If the mask bit is 1, the result bit will always be 1.  So you might also think of it as turning bits on.  I hope this doesn't confuse you, but another way to look at it is a simple 1-bit addition with saturation.  This is different than normal addition because each bit pair is processed individually (instead of all bits being considered a single number).
 
If both of the parameters are either 0 or -1 then the result will also be either -1 or 0, which is completely logical.  BASIC operators which return a boolean result always return -1 for true or 0 for false so this works well most of the time.  However, using OR with other non-zero values (besides -1) will typically produce non-logical values if the resullt is not zero.  This is why OR is more properly called a bit-wise boolean operator (instead of a logical operator).  Examples are provided below, but to really understand them, you need to know how to convert a decimal number into binary (and back again).  I won't try to explain that here; you may find this web page helpful for experimenting, or use a calculator.
 
The OR operator has the lowest priority.  Of course the user may invoke parentheses to change the order of evaluation.
 
Boolean/Logical operators (like ORl) are frequently used with IF, DO, and LOOP.
 
Some "logical" examples:
PRINT "A" OR "B"

?TYPE MISMATCH ERROR
READY.
A=1 : B=2

READY.
PRINT A>0 OR A>B
-1

READY.
PRINT A<0 OR A>B
 0

READY.
 
Some examples about operator priority:
A=1 : B=2 : PRINT A>0 OR A>B AND B<0
 1

READY.
PRINT (A>0 OR A>B) AND B<0
 0

READY.
 
Some examples demonstrating bit-wise complement:
A=5      : REM binary 0000 0000 0000 0101

READY.
B=3      : REM binary 0000 0000 0000 0011

READY.
PRINT A OR B
 7       : REM binary 0000 0000 0000 0111

READY.
PRINT NOT (A OR B)
-8       : REM the result is not logical!

READY.
 
  Compare With  
 
  Contrast With  
 
  See Also  

© H2Obsession, 2014
Comments