Sometimes. You can use it as a number and there can be good reasons to do so (like the documented strong zero property of false).
But if you’re just doing simple logical expressions, it’s usually far more readable to other humans to just use the logical operators. The above is just !.
I’m not sure that they always lower to the same thing. Have you tried with Float64 returns? There, it can make the difference between autovectorization kicking in and not. This example though is probably too simple to show any real difference.
The llvm is not exactly the same. I don’t know if it’s meaningfully different. I don’t really know how to read llvm.
I’ve heard people express concerns about excessive branching in if/else. I don’t understand what’s exactly sub-optimal about it. I thought that, if what’s behind the if/else conditions are numerical computations, using Bool values directly in those computations could be one way to get rid of branching. Though in simple cases like these, the compiler seems to do just fine with if/else.
Typically the compiler is smart enough these days to remove the branches if they’re not needed, just like you see above. The branchy version often gives the compiler more latitude to do what it thinks will be best. And branches — predictable ones — are often near-zero cost.
Branches do prevent SIMD and unpredictable branches can incur some fairly big slowdowns, so that’s where this becomes more important.
Write for humans first. Then profile and optimize if necessary.