Boolean Expressions (Advanced)

Top  Previous  Next

A boolean is a value that has two states: true or false. In XPL0, integers are used to represent booleans. Boolean expressions are formed by combining booleans with boolean operators.


XPL0 has four boolean operators: "not", "and", "or", and "exclusive or". The following symbols perform these operations:


        ~   "Not" operator (the word "not" can also be used)

        &   "And" operator

        !   "Or" operator

        |   "Exclusive or" operator


"Not" operates on a single value--it is another unary operator like the minus sign. It simply changes the value to its opposite. For instance, "not true" evaluates to "false". The "and" operator requires two values. If either value is false then the result is false. If both are true then the result is true. The "or" operator also requires two values. If both values are false then the result is false. If either value is true then the result is true. "Exclusive or" requires two values. If both values are the same then the result is false. If the values are different, the result is true. Here are some examples:


        if Pig = ~true then Text(0, "Still ok");

        if Guess<20 & Number>70 then Text(0, "Way too low");

        if Pig ! Bombed then Text(0, "Blew it!")


Boolean operators actually perform on all 16 bits of an integer at once. Here are some examples, using 4-bit values for simplicity:


        ~ 1100            1100            1100            1100

        = 0011          & 1010          ! 1010          | 1010

                        = 1000          = 1110          = 0110


Boolean operations are used to set and clear specific bits. A common operation is masking, which uses the "and" operator to clear all the bits except the ones we are interested in. For example, Number & 1 would tell us if Number is even or odd by masking off all but the least significant bit.


The value "true" is not limited to just $FFFF, but is defined as any non-zero value. Thus "anding" the odd number above with 1 is 1, which is "true". However be careful when using values other than $FFFF for "true". There can be instances when the "not" of a true value is not false. For example, ~$33 is $FFCC, both of which are non-zero, and thus both are "true".


Expressions can contain boolean operations, comparisons, and mathematical operations. In mixed expressions, arithmetic operations are done first, then comparisons, then boolean "not", then "and", "or", and "exclusive or". Therefore, the following expressions are the same:


        (A = 1) & (B = 2)   is the same as   A=1 & B=2

        (X & Y) ! Z         is the same as   X&Y ! Z


But these are different:


        (A & $80) = 0   is different than   A & $80=0

        ~(X ! Y)        is different than   ~X ! Y


A common mistake is to forget to use parenthesis when masking an expression such as this:


        Number & 7 = 3   is different than   (Number & 7) = 3


Boolean operations cannot be done on real numbers. For example, this gives a compile error:


        Frog & 3.2


However the following example is legal because the comparisons are done first, which produce true or false values for the "and" operator:


        Frog<3.2 & Toad>=6.3E3


Here are some more expressions using boolean operators:


        true & false            equals false

        $A ! 1                  equals $B

        false & false ! true    equals true

        false & (false ! true)  equals false

        ~$55AA & $F0F0          equals $A050

        ~($F0F ! $33)           equals $F0C0

        3+1 = 4                 equals true

        3=4 & true              equals false

        (1 ! $80) = $81         equals true (or $FFFF)

        1 ! $80 = $81           equals 1 (or true)

        4+1=6-1 & not 10>12     equals true

        17/3=5 & Rem(0)=2       equals true

        (A&~B ! ~A&B) = (A|B)   equals true