Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


10.4.3 Bitwise AND and OR

As hinted in the earlier chapters, the operators AND and OR are not simply logical operators. In fact, they are both bit-wise operators, where a bit is a binary digit (i.e., the zeroes or ones in the binary form of a number). So, to see how they work we should look at what happens to zeroes and ones:

 x  y   x OR y  x AND y
------------------------
 1  1     1        1
 1  0     1        0
 0  1     1        0
 0  0     0        0

Now, when you AND or OR two numbers the corresponding bits (binary digits) of the numbers are compared individually, according to the above table. So if x were %0111010 and y were %1010010 then x AND y would be %0010010 and x OR y would be %1111010:

        %0111010        %0111010
      AND             OR
        %1010010        %1010010
         -------         -------
        %0010010        %1111010

The numbers (in binary form) are lined up above each other, just like you do additions with normal numbers (i.e., starting with the right-hand digits, and maybe padding with zeroes on the left-hand side). The two bits in each column are AND-ed or OR-ed to give the result below the line.

So, how does this work for TRUE and FALSE, and logic operations? Well, FALSE is the number zero, so all the bits of FALSE are zeroes, and TRUE is -1, which has all 32 bits as ones (these numbers are LONG so they are 32-bit quantities). So AND-ing and OR-ing these values always gives numbers which have all zero bits (i.e., FALSE) or all one bits (i.e., TRUE), as appropriate. It's only when you start mixing numbers that aren't zero or -1 that you can muck up the logic. The non-zero numbers one and four are (by themselves) considered to be true, but 4 AND 1 is %100 AND %001 which is zero (i.e., false). So when you use AND as the logical operator it's not strictly true that all non-zero numbers represent true. OR does not give such problems so all non-zero numbers are treated as true. Run this example to see why you should be careful:

PROC main()
  test(TRUE,          'TRUE\t\t')
  test(FALSE,         'FALSE\t\t')
  test(1,             '1\t\t')
  test(4,             '4\t\t')
  test(TRUE OR TRUE,  'TRUE OR TRUE\t')
  test(TRUE AND TRUE, 'TRUE AND TRUE\t')
  test(1 OR 4,        '1 OR 4\t\t')
  test(1 AND 4,       '1 AND 4\t\t')
ENDPROC

PROC test(x, title)
  WriteF(title)
  WriteF(IF x THEN ' is TRUE\n' ELSE ' is FALSE\n')
ENDPROC

Here's the output that should be generated:

TRUE             is TRUE
FALSE            is FALSE
1                is TRUE
4                is TRUE
TRUE OR TRUE     is TRUE
TRUE AND TRUE    is TRUE
1 OR 4           is TRUE
1 AND 4          is FALSE

So, AND and OR are primarily bit-wise operators, but they can be used as logical operators under most circumstances, with zero representing false and all other numbers representing true. Care must be taken when using AND with some pairs of non-zero numbers, since the bit-wise AND of such numbers does not always give a non-zero (or true) result.

You can easily turn any value into a real truth value using the expression x<>FALSE, where x represents the value to be converted. For example, this expression is true: (1<>FALSE) AND (4<>FALSE).


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.