OR: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 4: | Line 4: | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: {{Parameter|result}} = firstValue [[OR]] secondValue | : {{Parameter|result}} = firstValue [[OR]] secondValue | ||
Line 16: | Line 15: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' OR always turns bits on! Never off. | ''Example 1:'' OR always turns bits on! Never off. | ||
{{CodeStart}} | {{CodeStart}} | ||
a% = 5 ' 101 binary | a% = 5 ' 101 binary | ||
b% = 4 ' 100 binary | b% = 4 ' 100 binary | ||
results% = a% {{Cl|OR}} b% ' still 101 binary using OR | results% = a% {{Cl|OR}} b% ' still 101 binary using OR | ||
{{Cl|PRINT}} "Results% ="; results% | {{Cl|PRINT}} "Results% ="; results% | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Results% = 5 | Results% = 5 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
''Example 2:'' Turning a data register bit on. | ''Example 2:'' Turning a data register bit on. | ||
{{CodeStart}} | {{CodeStart}} | ||
address% = 888 'parallel port data register | address% = 888 'parallel port data register | ||
bytevalue% = {{Cl|INP}}(address%) | bytevalue% = {{Cl|INP}}(address%) | ||
{{Cl|OUT}} address%, bytevalue% {{Cl|OR}} 4 | {{Cl|OUT}} address%, bytevalue% {{Cl|OR}} 4 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 38: | Line 37: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[AND]], [[XOR]] | * [[AND]], [[XOR]] | ||
* [[AND (boolean)]], [[OR (boolean)]] | * [[AND (boolean)]], [[OR (boolean)]] | ||
* [[Binary]], [[Boolean]] | * [[Binary]], [[Boolean]] |
Latest revision as of 00:52, 29 January 2023
The OR numerical operator returns a comparative bit value of 1 if either value's bit is on.
Syntax
- result = firstValue OR secondValue
Description
- If both bits are off, it returns 0.
- If one or both bits are on then it returns 1.
- OR never turns off a bit and can be used only to turn a bit on.
Table 4: The logical operations and its results. In this table, A and B are the Expressions to invert or combine. Both may be results of former Boolean evaluations. ┌────────────────────────────────────────────────────────────────────────┐ │ Logical Operations │ ├───────┬───────┬───────┬─────────┬────────┬─────────┬─────────┬─────────┤ │ A │ B │ NOT B │ A AND B │ A OR B │ A XOR B │ A EQV B │ A IMP B │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ true │ true │ false │ true │ true │ false │ true │ true │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ true │ false │ true │ false │ true │ true │ false │ false │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ false │ true │ false │ false │ true │ true │ false │ true │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ false │ false │ true │ false │ false │ false │ true │ true │ └───────┴───────┴───────┴─────────┴────────┴─────────┴─────────┴─────────┘ Note: In most BASIC languages incl. QB64 these are bitwise operations, hence the logic is performed for each corresponding bit in both operators, where true or false indicates whether a bit is set or not set. The outcome of each bit is then placed into the respective position to build the bit pattern of the final result value. As all Relational Operations return negative one (-1, all bits set) for true and zero (0, no bits set) for false, this allows us to use these bitwise logical operations to invert or combine any relational checks, as the outcome is the same for each bit and so always results into a true (-1) or false (0) again for further evaluations. |
Examples
Example 1: OR always turns bits on! Never off.
a% = 5 ' 101 binary b% = 4 ' 100 binary results% = a% OR b% ' still 101 binary using OR PRINT "Results% ="; results% |
Results% = 5 |
Example 2: Turning a data register bit on.
address% = 888 'parallel port data register bytevalue% = INP(address%) OUT address%, bytevalue% OR 4 |
- Explanation: The third register bit is only turned on if it was off. This ensures that a bit is set. OR could set more bits on with a sum of bit values such as: OUT address%, 7 would turn the first, second and third bits on. 1 + 2 + 4 = 7
See also