Suppose you want the Xor of two booleans, but the language does not have Xor – like Scheme or Lua 5.1. The obvious thing to do is just implement it – it is very simple, e.g in Scheme: (define (xor a b) (and (not (and a b)) (or a b)))
.
But if you are dealing with numbers there might be a neat short cut. Instead of:
/* in C: check directions are on same side of surface (no transmission) */
const bool isSameSide = !( (inDot < 0.0f) ^ (outDot < 0.0f) );
do:
/* in C: check directions are on same side of surface (no transmission) */
const bool isSameSide = (inDot * outDot) > 0.0;
Because multiplication of signs has the same truth-function shape as Xor:
exclusive-or | sign-multiply | ||||
---|---|---|---|---|---|
0 | 0 | 0 | - | - | + |
0 | 1 | 1 | - | + | - |
1 | 0 | 1 | + | - | - |
1 | 1 | 0 | + | + | + |
That is all.