Recent Changes - Search:

* PCAN

* Phix

edit SideBar

LogicalOperators

Index | Core Language | Expressions | Logical Operators

Logical Operators

The four logical operators and, or, xor, and not are used to determine the "truth" of an expression. e.g.

        1 and 1     -- 1 (true)
        1 and 0     -- 0 (false)
        0 and 1     -- 0 (false)
        0 and 0     -- 0 (false)
        1 or 1      -- 1 (true)
        1 or 0      -- 1 (true)
        0 or 1      -- 1 (true)
        0 or 0      -- 0 (false)
        1 xor 1     -- 0 (false)
        1 xor 0     -- 1 (true)
        0 xor 1     -- 1 (true)
        0 xor 0     -- 0 (false)
        not 1       -- 0 (false)
        not 0       -- 1 (true)

Function-style equivalents of these operators can also be applied to entire sequences, as explained shortly in Sequence Operations, eg sq_and({true,{false,true}},{true,false}) ==> {true,{false,false}}.

In all cases short-circuit evaluation is used for expressions containing and or or.

The constants TRUE, True, true, FALSE, False, and false are automatically builtin to the Phix compiler. There is no difference between the different cases, which makes for one less thing you have to remember.

You may also find these operators applied to numbers other than 1 or 0. The rule is: zero means false and non-zero means true. So for instance, replacing variable names (which might make more sense) with literal values to show exactly what is going on:

        5 and -4    -- 1 (true)
        not 6       -- 0 (false)

One could argue the above is relatively poor practice. The equivalent expressions "5!=0 and -4!=0" and "6=0" yield the same results and (at least when using sensible variable names rather than those meaningless literals) can communicate the intent with greater clarity. If there are any cases where the latter forms generate slightly less efficient code, that should be logged as a bug in the compiler. There are, however, countless instances of this sort of practice in legacy code, far too many to ever fully eradicate, especially given that attempts to do so are likely to introduce new bugs (even if they are schrödinbugs1).

One case worth mentioning is the popular idiom [not] find(needle,haystack), for which encouraging the programmer to tack "=0" or "!=0" onto the end does not really improve anything, and smacks of pedantry and pettiness, but of course there is exactly the same 0 means false and anything else means true.

Parenthesis is required to mix different logical operators in a single expression, as explained below the Precedence Chart.

See Also: and_bits?, or_bits?, xor_bits?, and not_bits?, which are the corresponding bitwise functions.
Technicalia 1schrödinbug: a bug that occurs after the programmer notices that the code should never have worked in the first place, then typically manifests itself all over the shop, hampering attempts to rectify it, for reasons you can never quite grasp and despite having passed all tests with flying colours for donkey’s years. Not to be confused with a heisenbug: a bug that disappears when one attempts to study it. (A cute name but an incorrect reference. The quantum mechanical property of things changing when you observe them is the observer effect, not the Heisenberg uncertainty principle, however observereffectbug is simply just not as cute or funny.)
 

< Relational Operators | Index | Arithmetic Operators >

Edit - History - Print - Recent Changes - Search
Page last modified on April 16, 2026, at 03:13 PM