@@ -36,9 +36,21 @@ namespace Ternary
3636{
3737 using ResultType = UInt8;
3838
39- static constexpr UInt8 False = 0 ;
40- static constexpr UInt8 True = -1 ;
41- static constexpr UInt8 Null = 1 ;
39+ /* * These carefully picked values magically work so bitwise "and", "or" on them
40+ * corresponds to the expected results in three-valued logic.
41+ *
42+ * False and True are represented by all-0 and all-1 bits, so all bitwise operations on them work as expected.
43+ * Null is represented as single 1 bit. So, it is something in between False and True.
44+ * And "or" works like maximum and "and" works like minimum:
45+ * "or" keeps True as is and lifts False with Null to Null.
46+ * "and" keeps False as is and downs True with Null to Null.
47+ *
48+ * This logic does not apply for "not" and "xor" - they work with default implementation for NULLs:
49+ * anything with NULL returns NULL, otherwise use conventional two-valued logic.
50+ */
51+ static constexpr UInt8 False = 0 ; // / All zero bits.
52+ static constexpr UInt8 True = -1 ; // / All one bits.
53+ static constexpr UInt8 Null = 1 ; // / Single one bit.
4254
4355 template <typename T>
4456 inline ResultType makeValue (T value)
@@ -61,9 +73,16 @@ struct AndImpl
6173 using ResultType = UInt8;
6274
6375 static inline constexpr bool isSaturable () { return true ; }
76+
77+ // / Final value in two-valued logic (no further operations with True, False will change this value)
6478 static inline constexpr bool isSaturatedValue (bool a) { return !a; }
79+
80+ // / Final value in three-valued logic (no further operations with True, False, Null will change this value)
6581 static inline constexpr bool isSaturatedValueTernary (UInt8 a) { return a == Ternary::False; }
82+
6683 static inline constexpr ResultType apply (UInt8 a, UInt8 b) { return a & b; }
84+
85+ // / Will use three-valued logic for NULLs (see above) or default implementation (any operation with NULL returns NULL).
6786 static inline constexpr bool specialImplementationForNulls () { return true ; }
6887};
6988
0 commit comments