Skip to content

Boolean Operations

Bill Hails edited this page Nov 23, 2018 · 13 revisions
true and false;

is false. You receive similarly sensible results for or, not and xor, and comparison operators return boolean values:

10 < 12 and 12 < 14; // true

Evaluation of boolean operators is short circuited appropriately so true or something will not evaluate something.

One surprise, booleans can have a third value: unknown. Truth tables for the boolean operators involving unknown are as follows:

and

lhs rhs result
true true true
true unknown unknown
true false false
unknown true unknown
unknown unknown unknown
unknown false false
false true false
false unknown false
false false false

or

lhs rhs result
true unknown true
true true true
true false true
unknown unknown unknown
unknown true true
unknown false unknown
false unknown unknown
false true true
false false false

xor

lhs rhs result
true true false
true unknown unknown
true false true
unknown true unknown
unknown unknown unknown
unknown false unknown
false true true
false unknown unknown
false false false

not

rhs result
true false
unknown unknown
false true

Of special interest is that certain operations involving unknown do not result in unknown. Specifically false and unknown is false, as false and anything will always be false; likewise true or unknown is true.

unknown cannot result from any boolean operation on known truth values and (so far) no comparison operators result in unknown, so unless you explicitly introduce the value you do not need to know about it.

Equality of booleans in a three-valued logic system could be tricky, specifically if unknown means "either true or false" then any_boolean == unknown should be unknown, but given we need a way to test for unknown, I've chosen the simpler option where true == true, false == false and unknown == unknown.

This makes possible things like result == unknown and back (see later for then and back) which might be useful in decidability algorithms. If you actually want that more "correct" behaviour when comparing booleans with unknown, you can replace a == b with not (a xor b).

Comparison Operators

The standard comparison operators are available. They require that the types of their arguments agree, and all types can be compared (sometimes somewhat arbitrarily, for example false < unknown and unknown < true.) The operators are:

op name
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to

Up: Home

Next: Lists

Clone this wiki locally