3 ms·
That would only be true if `a` is guaranteed to be a non-zero value though, right? e.g. `x = some_bool ? 0 : 1` would not be able to be represented that way.
by eridius 8y ago
That would only be true if `a` is guaranteed to be a non-zero value though, right? e.g. `x = some_bool ? 0 : 1` would not be able to be represented that way.
- repsilat 8y agoIt works for me. Note that `|` is a bitwise-or (not that it really matters, it just removes a branch) const True = -1 const False = 0 const ternary = (condition, left, right) => \ (condition & left) | (~condition & right) ternary(True, 0, 1) // 0 ternary(False, 0, 1) // 1
- eridius 8y agoOh right, because the right-hand side evaluates to 0 as well in this case. Makes sense.