AND: 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
(Created page with "The logical AND numerical operator compares two values in respect of their bits. If both bits at a certain position in both values are set, then that bit position is set in the result. {{PageSyntax}} :{{Parameter|result}} = {{Parameter|firstvalue}} AND {{Parameter|secondvalue}} {{PageDescription}} * AND compares the bits of the {{Parameter|firstvalue}} against the bits of the {{Parameter|secondvalue}}, the result is stored in the {{Parameter|result}} variable...") Tag: visualeditor-switched |
No edit summary |
||
(9 intermediate revisions by the same user not shown) | |||
Line 12: | Line 12: | ||
* AND is often used to see if a bit is on by comparing a value to an exponent of 2. | * AND is often used to see if a bit is on by comparing a value to an exponent of 2. | ||
* Can turn off a bit by subtracting the bit on value from 255 and using that value to AND a byte value. | * Can turn off a bit by subtracting the bit on value from 255 and using that value to AND a byte value. | ||
{{LogicalTruthPlugin}} | |||
{{ | |||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' | ''Example 1:'' | ||
{{ | {{TextStart}} | ||
101 | 101 | ||
AND | AND | ||
Line 25: | Line 23: | ||
----- | ----- | ||
001 | 001 | ||
{{ | {{TextEnd}} | ||
:The 101 bit pattern equals 5 and the 011 bit pattern equals 3, it returns the bit pattern 001 which equals 1. Only the Least Significant Bits (LSB) match. So decimal values 5 AND 3 = 1. | :The 101 bit pattern equals 5 and the 011 bit pattern equals 3, it returns the bit pattern 001 which equals 1. Only the Least Significant Bits (LSB) match. So decimal values 5 AND 3 = 1. | ||
Line 31: | Line 29: | ||
''Example 2:'' | ''Example 2:'' | ||
{{ | {{TextStart}} | ||
11111011 | 11111011 | ||
AND | AND | ||
Line 37: | Line 35: | ||
---------- | ---------- | ||
11101011 | 11101011 | ||
{{ | {{TextEnd}} | ||
:Both bits have to be set for the resulting bit to be set. You can use the [[AND]] operator to get one byte of a two byte integer this way: | :Both bits have to be set for the resulting bit to be set. You can use the [[AND]] operator to get one byte of a two byte integer this way: | ||
Line 47: | Line 45: | ||
''Example 3:'' Finding the binary bits on in an [[INTEGER]] value. | ''Example 3:'' Finding the binary bits on in an [[INTEGER]] value. | ||
{{CodeStart}} | {{CodeStart}} | ||
DO | DO | ||
{{Cl|INPUT}} "Enter Integer value from -32768 to 32767 (Enter quits): ", INTvalue& | {{Cl|INPUT}} "Enter Integer value from -32768 to 32767 (Enter quits): ", INTvalue& | ||
IF INTvalue& < -32768 OR INTvalue& > 32767 OR INTval& = 0 THEN {{Cl|EXIT DO}} | IF INTvalue& < -32768 OR INTvalue& > 32767 OR INTval& = 0 THEN {{Cl|EXIT DO}} | ||
{{Cl|FOR...NEXT|FOR}} exponent = 15 {{Cl|TO}} 0 {{Cl|STEP}} -1 | {{Cl|FOR...NEXT|FOR}} exponent = 15 {{Cl|TO}} 0 {{Cl|STEP}} -1 | ||
Line 69: | Line 67: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[OR]], [[XOR]], [[NOT]] {{ | * [[OR]], [[XOR]], [[NOT]] {{Text|(logical operators)}} | ||
* [[AND (boolean)]] | * [[AND (boolean)]] | ||
* [[Binary]], [[Boolean]] | * [[Binary]], [[Boolean]] |
Latest revision as of 22:11, 11 February 2023
The logical AND numerical operator compares two values in respect of their bits. If both bits at a certain position in both values are set, then that bit position is set in the result.
Syntax
- result = firstvalue AND secondvalue
Description
- AND compares the bits of the firstvalue against the bits of the secondvalue, the result is stored in the result variable.
- If both bits are on (1) then the result is on (1).
- All other conditions return 0 (bit is off).
- AND is often used to see if a bit is on by comparing a value to an exponent of 2.
- Can turn off a bit by subtracting the bit on value from 255 and using that value to AND a byte value.
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:
101 AND 011 ----- 001 |
- The 101 bit pattern equals 5 and the 011 bit pattern equals 3, it returns the bit pattern 001 which equals 1. Only the Least Significant Bits (LSB) match. So decimal values 5 AND 3 = 1.
Example 2:
11111011 AND 11101111 ---------- 11101011 |
- Both bits have to be set for the resulting bit to be set. You can use the AND operator to get one byte of a two byte integer this way:
- firstbyte = twobyteint AND 255
- Since 255 is 11111111 in binary, it will represent the first byte completely when compared with AND.
- To find the second (HI) byte's decimal value of two byte INTEGERs use: secondbyte = twobyteint \ 256
Example 3: Finding the binary bits on in an INTEGER value.
DO INPUT "Enter Integer value from -32768 to 32767 (Enter quits): ", INTvalue& IF INTvalue& < -32768 OR INTvalue& > 32767 OR INTval& = 0 THEN EXIT DO FOR exponent = 15 TO 0 STEP -1 IF (INTvalue& AND 2 ^ exponent) THEN PRINT "1"; ELSE PRINT "0"; NEXT PRINT " " LOOP UNTIL INTvalue& = 0 'zero entry quits |
- Example output for 6055.
0001011110100111 |
- Note: The value of 32767 sets 15 bits. -1 sets all 16 bits. Negative values will all have the highest bit set. Use LONG variables for input values to prevent overflow errors.
See also