1. 1
  1.  

  2. 1

    I’d be extremely wary using ternary in anything I wanted to be branchless. The result can be arbitrary expressions, so theres no way for it to be branchless in general. Writing it out with multiplication is the safer way to go.

    1. 2

      I’ve always assumed that in general, the ternary operator compiles to a conditional jump just like an if` statement. (Some languages actually use the “if” keyword in their ternary operator, making this even more explicit.) Obviously the compiler may optimize it, but I’d assume that would happen in the equivalent if statement as well. Is that wrong? Are there optimizations possible in a ternary operator that are not possible in an if statement?